我正在尝试将FancyArrowPatch添加到子图,并根据度数顺序旋转图块。我尝试了变换的几种变体,但是没有一个可行的解决方案。 This solution令我赞叹,好像它应该可以工作,但是当我添加转换时,补丁消失了(转换运行没有错误)。我也尝试过mpl.patches.arrow()
。
我不愿意使用FancyArrowPatch-任何形式的方向标记都足够-只要它们可以独立旋转以反映风向即可。我仅限于matplotlib 1.3.1;在这种情况下,不能选择迁移到新版本。我强烈怀疑这与以下事实有关:x轴是基于时间的,并且/或者由于收集而涉及某种方式的混乱,但这只是一个猜测。
# -*- coding: utf-8 -*-
import datetime as dt
import matplotlib.pyplot as plt # vers 1.3.1
import matplotlib.dates as md
import matplotlib.patches as mpatches
import matplotlib.transforms as transform
x_offset = dt.timedelta(hours=4)
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharex=True)
x0 = [1558065600, 1558152000, 1558238400, 1558324800, 1558411200, 1558497600, 1558584000, 1558670400] # <-- Date
x1 = [dt.datetime.fromtimestamp(_) for _ in x0]
# ================================= Subplot 1 =================================
y = [83.12, 82.45, 87.51, 85.27, 75.85, 81.72, 91.16, 86.74] # <-- Temperature
ax0.set_title('high temperature')
ax0.plot(x1, y)
ax0.set_xlim(min(x1) - x_offset, max(x1) + x_offset)
myFmt = md.DateFormatter('%m-%d')
ax1.xaxis.set_major_formatter(myFmt)
# ================================= Subplot 2 =================================
y = [4.25, 2.03, 7.77, 9.85, 6.73, 4.47, 5.97, 7.24] # <-- Wind Speed
z = [251, 86, 171, 224, 331, 147, 265, 319] # <-- Wind Bearing
data = zip(x1, y, z)
ax1.set_title('wind')
ax1.plot(x1, y)
ax1.set_ylim(0, max(y) + 1)
for _ in data:
day = md.date2num(_[0])
location = _[1]
bearing = _[2] # <-- Rotate patch based on this
arr = mpatches.FancyArrowPatch(posA=(day, location), posB=(day, location + 0.1), arrowstyle='Simple', color='red', lw=0)
arr.set_arrowstyle("fancy", head_length=10, head_width=10)
# <-- Perform elusive transformation -->
ax1.add_patch(arr)
# ================================= Subplot 3 =================================
# Precip intensity is in inches of liquid rain per hour.
y = [_ * 24 for _ in [0.0021, 0.0035, 0.0018, 0.0017, 0.0001, 0.0000, 0.0001, 0.0003]] # <-- Precip
ax2.set_title('precip intensity')
ax2.plot(x1, y)
plt.show()
基于Sheldore的建议(示例1),我能够找到解决方案。新代码的关键部分取代了对FancyArrowPatch的调用。
# -*- coding: utf-8 -*-
import datetime as dt
import matplotlib.pyplot as plt # vers 1.3.1
import matplotlib.dates as md
import matplotlib.patches as mpatches
import matplotlib.transforms as transform
x_offset = dt.timedelta(hours=4)
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharex=True)
x0 = [1558065600, 1558152000, 1558238400, 1558324800, 1558411200, 1558497600, 1558584000, 1558670400] # <-- Date
x1 = [dt.datetime.fromtimestamp(_) for _ in x0]
# ================================= Subplot 1 =================================
y = [83.12, 82.45, 87.51, 85.27, 75.85, 81.72, 91.16, 86.74] # <-- Temperature
ax0.set_title('high temperature')
ax0.plot(x1, y)
ax0.set_xlim(min(x1) - x_offset, max(x1) + x_offset)
myFmt = md.DateFormatter('%m-%d')
ax1.xaxis.set_major_formatter(myFmt)
# ================================= Subplot 2 =================================
y = [4.25, 2.03, 7.77, 9.85, 6.73, 4.47, 5.97, 7.24] # <-- Wind Speed
z = [251, 86, 171, 224, 331, 147, 265, 319] # <-- Wind Bearing
data = zip(x1, y, z)
ax1.set_title('wind')
ax1.plot(x1, y)
ax1.set_ylim(0, max(y) + 1)
for _ in data:
day = md.date2num(_[0])
location = _[1]
# New code to replace FancyArrowPatch
ax1.annotate('', xy=(day, location), xycoords='data', xytext=(0, 1), textcoords='offset points', rotation=0, size=20,
arrowprops=dict(arrowstyle="simple", fc="red", ec="none", connectionstyle="angle3,angleA=0,angleB={0}".format(_[2])))
# ================================= Subplot 3 =================================
# Precip intensity is in inches of liquid rain per hour.
y = [_ * 24 for _ in [0.0021, 0.0035, 0.0018, 0.0017, 0.0001, 0.0000, 0.0001, 0.0003]] # <-- Precip
ax2.set_title('precip intensity')
ax2.plot(x1, y)
plt.show()
我需要稍微清理一下,但是解决方案会产生预期的结果。