繁体   English   中英

如何使用 matplotlib 将注释堆叠在一起?

[英]How can I stack annotations on top of each other with matplotlib?

我想生成一个沿两个轴进行矢量分解的图形(请参见下面的示例),并且我正在使用matplotlib.axes.Axes.annotate() ( docs ) 来执行此操作。 看起来 matplotlib 会自动移动红色小箭头的尖端,以免它们落在较大的黑色箭头上。 我试过查看文档,但找不到解决方案。 我也会接受其他图书馆作为答案。

from matplotlib import pyplot as plt

fig, ax = plt.subplots(1,1)

# Square aspect ratio
ax.set_aspect(1)
ax.set(xlim=[-1,1], ylim=[-1,1], xticks=[], yticks=[])

# Draw image
ax.annotate('f', (-1, 0), (1, 0), arrowprops=dict(arrowstyle='<-'))
ax.annotate('s', (0, -1), (0, 1), arrowprops=dict(arrowstyle='<-'))
ax.annotate('', (0, 0), (.5, .5), arrowprops=dict(arrowstyle='<-', color='r'))    
ax.annotate('', (0, 0), (0, .5), arrowprops=dict(color='r',arrowstyle='<-', ls='--'))
ax.annotate('', (0, 0), (.5, 0), arrowprops=dict(color='r',arrowstyle='<-', ls='--'))

此代码生成此图像

看起来这与箭头的 alignment 和注释文本有关。 不过,我不确定这是一个错误,还是故意制造的。

相比:

fig, (ax1,ax2) = plt.subplots(1,2, figsize=(8,4))
for ax in [ax1,ax2]:
    ax.set(xlim=[-1,1], ylim=[-0.1,0.1], xticks=[], yticks=[])
ax1.annotate('A', (-1, 0), (1, 0), arrowprops=dict(arrowstyle='<-', color='k'))
ax1.annotate('', (-1, 0), (1, 0), arrowprops=dict(color='r',arrowstyle='<-', ls='--'))

ax2.annotate('', (-1, 0), (1, 0), arrowprops=dict(arrowstyle='<-', color='k'))
ax2.annotate('', (-1, 0), (1, 0), arrowprops=dict(color='r',arrowstyle='<-', ls='--'))

在此处输入图像描述

因此,我对您的解决方案是从您拥有的箭头注释中删除文本,并使用单独的注释或ax.text()添加它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM