我试图在matplotlib中的轴的末端设置一个箭头。 我不想删除刺和替换为纯箭头,因为我需要他们的功能。。。
我的实现is as slight modification of joferkington implementation
import matplotlib.pyplot as plt
import numpy as np
def arrowed_spines(ax=None, arrowLength=30, labels=('X', 'Y'), arrowStyle='<|-'):
xlabel, ylabel = labels
for i, spine in enumerate(['left', 'bottom']):
# Set up the annotation parameters
t = ax.spines[spine].get_transform()
xy, xycoords = [1, 0], ('axes fraction', t)
xytext, textcoords = [arrowLength, 0], ('offset points', t)
# create arrowprops
arrowprops = dict( arrowstyle=arrowStyle,
facecolor=ax.spines[spine].get_facecolor(),
linewidth=ax.spines[spine].get_linewidth(),
alpha = ax.spines[spine].get_alpha(),
zorder=ax.spines[spine].get_zorder(),
linestyle = ax.spines[spine].get_linestyle() )
if spine is 'bottom':
ha, va = 'left', 'center'
xarrow = ax.annotate(xlabel, xy, xycoords=xycoords, xytext=xytext,
textcoords=textcoords, ha=ha, va='center',
arrowprops=arrowprops)
else:
ha, va = 'center', 'bottom'
yarrow = ax.annotate(ylabel, xy[::-1], xycoords=xycoords[::-1],
xytext=xytext[::-1], textcoords=textcoords[::-1],
ha='center', va=va, arrowprops=arrowprops)
return xarrow, yarrow
# plot
x = np.arange(-2., 10.0, 0.01)
plt.plot(x, x**2)
plt.gcf().set_facecolor('white')
ax = plt.gca()
ax.set_xticks([])
ax.set_yticks([])
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
arrowed_spines(ax)
plt.show()
似乎一个或两个点的移动在开始位置和箭头与脊椎的对齐上是一致的。我不知道怎么解决这个问题。任何帮助都将不胜感激。
谢谢
为了得到你想要的,在从arrowed_spines函数中删除以下部分之后,Julien的答案就足够了:
在包含箭头之后,仍然可以修改脊椎,如您所见:
在这里,我结合了Julien和s3b4s的现有答案,使函数更通用,以便您可以指定要修改的轴和箭头的方向。
结果:
未决问题:我试图匹配现有脊椎的线宽,但由于某些原因,箭头的线条似乎较粗。实验表明,0.8的脊椎线宽与0.3左右的箭头线宽匹配。不确定原因-当前必须将
lw=<value>
设置为手动修复。您可以删除所有尖刺并展开箭头以覆盖数据范围(找到此代码here):
相关问题 更多 >
编程相关推荐