如何在Matplotlib中旋转花式箭头

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Matplotlib中旋转花式箭头相关的知识,希望对你有一定的参考价值。

如何旋转Matplotlib中的花式箭头(我不想动画),我尝试旋转,转换,它们都不适合我,我想,我做错了,请任何帮助或替代方案解决方案表示赞赏,我已粘贴下面的代码。

import matplotlib.pyplot as plt


fig, ax = plt.subplots()

#Arrow in degree
#Arrow represent the wind direction
degree= 45 #Arrow should rotate as per the specified degree, (0 degree is North)

#Draw Circle
#CENTER POINT
Circle1 = plt.Circle((5, 5), 0.1, color='blue', fill=True)
ax.add_artist(Circle1)
#CIRCLE
Circle2 = plt.Circle((5, 5), 6, color='blue', fill=False)
ax.add_artist(Circle2)

x3=5.0
y3=6.8
x4=5.0
y4=9.0  

#Wind Direction Arrow
#LEFT ARROW
ax.annotate('',
    xy=(x3-0.5, y3), xycoords='data',
    xytext=(-20, 50), textcoords='offset points', rotation=degree,
    size=20,
    # bbox=dict(boxstyle="round", fc="0.8"),
    arrowprops=dict(arrowstyle="fancy",
                    fc="0.6", ec="none",
                    connectionstyle="angle3,angleA=0,angleB=-90"))
#CENTER ARROW
ax.annotate('',
    xy=(x3, y3), xycoords='data',
    xytext=(0, 50), textcoords='offset points', rotation=degree,
    size=20,
    # bbox=dict(boxstyle="round", fc="0.8"),
    arrowprops=dict(arrowstyle="fancy",
                    fc="0.6", ec="none",
                    connectionstyle="angle3,angleA=0,angleB=-90"))
#RIGHT ARROW
ax.annotate('',
    xy=(x3+0.5, y3), xycoords='data',
    xytext=(20, 50), textcoords='offset points', rotation=degree,
    size=20,
    # bbox=dict(boxstyle="round", fc="0.8"),
    arrowprops=dict(arrowstyle="fancy",
                    fc="0.6", ec="none",
                    connectionstyle="angle3,angleA=0,angleB=-90"))

ax.set_aspect('equal') 
ax.set_xlim([-2.5,12.5])
ax.set_ylim([-3,15])
plt.show()

代码结果。 enter image description here

我的要求:

所有3箭头(箭头表示风向)应旋转到指定的度数,保持中心点为锚点。

答案

由于箭头和尾部是在不同的坐标系中指定的,我似乎唯一的选择是围绕圆心旋转坐标。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtrans

fig, ax = plt.subplots()

#Arrow represent the wind direction
#Arrow should rotate as per the specified degree, (0 degree is North)
x3=5.0
y3=5.0
#Draw Circle
#CENTER POINT
Circle1 = plt.Circle((x3, y3), 0.1, color='blue', fill=True)
ax.add_artist(Circle1)
#CIRCLE
Circle2 = plt.Circle((x3, y3), 6, color='blue', fill=False)
ax.add_artist(Circle2)


def winddirectionarrow(ax, xy, deg):
    m1 = np.array( (-1, 1) )
    m2 = np.array( (0, 1) )
    s1 = np.array( (0.5, 1.8) )
    s2 = np.array( (20, 50) )
    xy = np.array(xy)
    rot = mtrans.Affine2D().rotate_deg(deg)
    #Wind Direction Arrow
    cncs = "angle3,angleA={},angleB={}".format(deg,deg-90)
    kw = dict(xycoords='data',textcoords='offset points',size=20,
              arrowprops=dict(arrowstyle="fancy", fc="0.6", ec="none",
                              connectionstyle=cncs))
    #LEFT ARROW
    ax.annotate('', xy=xy + rot.transform_point(m1*s1), 
                    xytext=rot.transform_point(m1*s2), **kw)
    #CENTER ARROW
    ax.annotate('', xy=xy + rot.transform_point(m2*s1), 
                    xytext=rot.transform_point(m2*s2), **kw)
    #RIGHT ARROW
    ax.annotate('', xy=xy + rot.transform_point(s1), 
                    xytext=rot.transform_point(s2), **kw)

winddirectionarrow(ax, (x3,y3),  45)
winddirectionarrow(ax, (x3,y3), -60)
winddirectionarrow(ax, (x3,y3), 170)

ax.set_aspect('equal') 
ax.set_xlim([-2.5,12.5])
ax.set_ylim([-3,15])
plt.show()

enter image description here

以上是关于如何在Matplotlib中旋转花式箭头的主要内容,如果未能解决你的问题,请参考以下文章

汉堡菜单旋转到新活动的箭头

如何在matplotlib中用旋转矩阵把一个图形旋转

为啥代码片段在 matplotlib 2.0.2 上运行良好,但在 matplotlib 2.1.0 上引发错误

在 Matplotlib 中旋转图像

如何在活动的 Bootstrap 4 下拉菜单中旋转箭头图标?

Matplotlib 绘图细节设置