Python 使用 matplotlib 注释绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15148149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:28:34  来源:igfitidea点击:

annotate a plot using matplotlib

pythonmatplotlib

提问by Helen Firs

I am using this to making a plot with contains in specific moment something such as highlight

我正在使用它来制作包含特定时刻的情节,例如突出显示

plt.axis('normal')
plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.plot(ts.e1, 'r',linewidth=1.5)
plt.ylabel("Anger",fontsize=14,color='blue')
plt.ylim(ymax=0.04)
plt.xlim(xmax=122)
plt.grid(True)
plt.title("Anger - Real Events", fontsize=20,color='black')
plt.xlabel('minutes', fontsize=14, color='b')
plt.show()

Is there any idea if i could add horizontal text like 'span 1' in this:

有什么想法我可以在这个中添加像“span 1”这样的水平文本:

plt.axvspan(76, 76, facecolor='g', alpha=1)

or using the text 'line1' and have a arrow to show in this span?

或者使用文本“line1”并在此跨度中显示一个箭头?

回答by alecxe

Here's how you can have an arrow showing to the span:

以下是显示跨度的箭头的方法:

import matplotlib.pyplot as plt

plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.annotate('This is awesome!', 
             xy=(76, 0.75),  
             xycoords='data',
             textcoords='offset points',
             arrowprops=dict(arrowstyle="->"))
plt.show()

For more info about annotate see docs.

有关注释的更多信息,请参阅文档

The output of the above code: enter image description here

上面代码的输出: 在此处输入图片说明