有没有一种方法可以优雅地画出一个圆里面的箭头

2024-05-23 14:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在单位圆上画一个单位向量。你知道吗

这是密码

vunit = 1/np.sqrt(2)
vec1 = [vunit,vunit]
thetas = np.arange(-np.pi, np.pi, .05)
coordinates = np.vstack((np.cos(thetas),np.sin(thetas)))

plt.figure(figsize = (6,6))
plt.xlim(-3,3)
plt.ylim(-3,3)
plt.scatter(coordinates[0,:],coordinates[1,:],s=.1)
plt.arrow(0, 0, vec1[0], vec1[1], head_width=0.15, color='r')

enter image description here

一切正常,只是箭头在圆圈外。你知道吗

所以,我修改了vec1

vec1 = [vunit-.1,vunit-.1]

enter image description here

图看起来更好,我可以修改vec1更精细,但修复似乎是丑陋的。有没有办法让箭头优雅地在圆圈内


Tags: 密码nppi单位pltsqrt箭头coordinates
2条回答

使用^{}

import numpy as np
import matplotlib.pyplot as plt

vunit = 1/np.sqrt(2)
vec1 = [vunit,vunit]
thetas = np.arange(-np.pi, np.pi, .05)
coordinates = np.vstack((np.cos(thetas),np.sin(thetas)))

plt.figure(figsize = (6,6))
plt.xlim(-3,3)
plt.ylim(-3,3)
plt.scatter(coordinates[0,:],coordinates[1,:],s=.1)
plt.arrow(0, 0, vec1[0], vec1[1], head_width=0.15, color='r', length_includes_head=True)
plt.show()

enter image description here

可以使用^{}而不是FancyArrow(这是由plt.arrow生成的对象)。
这里的区别是微不足道的,但对于其他情况,因此一致性,FancyArrowPatch提供了许多FancyArrow所没有的好特性。在缩放绘图时可以观察到一个主要的区别;FancyArrow的头部是在数据坐标中定义的,因此在非等宽图中显示时看起来是倾斜的。你知道吗

enter image description here

下面是带有FancyArrowPatch的完整代码,我们通过shrinkB参数得到末端坐标处的头尖。你知道吗

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch

vunit = 1/np.sqrt(2)
vec1 = [vunit,vunit]
thetas = np.arange(-np.pi, np.pi, .05)
coordinates = np.vstack((np.cos(thetas),np.sin(thetas)))

plt.figure(figsize = (6,6))
plt.xlim(-3,3)
plt.ylim(-3,3)
plt.scatter(coordinates[0,:],coordinates[1,:],s=.1)

arrow = FancyArrowPatch(posA=(0,0), posB=vec1, 
                        arrowstyle='-|>', mutation_scale=20, 
                        shrinkA=0, shrinkB=0, color='r')
plt.gca().add_patch(arrow)

plt.show()

enter image description here

相关问题 更多 >