Python matplotlib-如何向垂直线添加注释

问题描述 投票:0回答:1

我绘制了一个时间序列图,并添加了一条垂直线。此垂直线表示发生了某些事情,我想在该垂直线上添加注释。但是我不知道该怎么做:/

这里是绘制数据和垂直线的代码:

我调整了现在包含数据的示例代码(尽管它们有些怪异,但显示了问题:垂直线处的符号应位于绘图区域上方,并且两个文本框现在重叠。这种重叠应该避免通过不同的方式排列它们,并使用虚线或其他东西指向文本所属的垂直线。

%matplotlib inline
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = np.array([['2017-07-01 16:30:00', '2017-07-02 16:30:00', '2017-07-03 16:30:00', '2017-07-04 16:30:00', '2017-07-05 16:30:00', '2017-07-06 16:30:00'],
                [1.4, 1.3, 2, 0.5, 0.002337, 3 ]])

fig2 = plt.figure(figsize=(16,9))

# TRAIN NORTH
ax0 = fig2.add_subplot(111)
ax0.scatter(x=data[0],y=data[1], color='green', marker='.')
#ax0.legend(df.iloc[0,0],loc='best')
#ax0.set_ylim(0,0.006)
# vertical lines when something happened
ax0.axvline('2017-07-02 16:30:00', color='green')
ax0.axvline('2017-07-02 16:30:00', color='green')
ax0.axvline('2017-07-05 16:30:00', color='green')
ax0.text('2017-07-02 16:30:00',0.005,'BigNews1',rotation=90,va='top')
ax0.text('2017-07-02 16:30:00',0.005,'BlaBlaBla2',rotation=90,va='top')
ax0.text('2017-07-05 16:30:00',0.005,'BigNews3',rotation=90,va='top')

I would like to have something like shown in this picture

他们使用包lineid_plot可以在here中找到

但是,此程序包在某种程度上存在日期时间格式的问题。对于此处显示的错误示例代码,我真的感到很抱歉,但是我确实是一个初学者,我希望somebaody可以帮助解决我的问题。

python matplotlib annotations time-series data-annotations
1个回答
0
投票

这是一些显示annotation的代码,类似于所请求的代码。

代码说明:

  • [ymin, ymax = plt.ylim()找到y轴的当前限制,ymax用于放置文本]
  • arrowprops = {'width': 1, 'headwidth': 1, 'headlength': 1, 'shrink':0.05}从该行的顶部到文本本身绘制了一个“箭头”。将“ headwidth”设置为1使其成为一条线,而不是箭头。一个人可以在没有重叠的地方画一条真实的箭头。
  • ax0.annotate('BigNews1',
    • xy=('2017-07-02 16:30:00', ymax),垂直线的顶部
    • [xytext=(10, 25), textcoords='offset points',相对于顶部的文本位置,以“点”为单位(与字体大小比较为12点)
    • [rotation=90, va='bottom', ha='center',文本旋转了90度
    • [annotation_clip=False,允许在图的框外书写
    • arrowprops=arrowprops)设置“箭头”的属性

仍然缺少的是一种在重叠时移动xytext的好算法。

import matplotlib.pyplot as plt
import numpy as np

data = np.array([['2017-07-01 16:30:00', '2017-07-02 16:30:00', '2017-07-03 16:30:00', '2017-07-04 16:30:00', '2017-07-05 16:30:00', '2017-07-06 16:30:00'],
                [1.4, 1.3, 2, 0.5, 0.002337, 3 ]])

fig2 = plt.figure(figsize=(16,9))
ax0 = fig2.add_subplot(111)
ax0.scatter(x=data[0],y=data[1], color='green', marker='.')
ymin, ymax = plt.ylim()
# vertical lines when something happened
ax0.axvline('2017-07-02 16:30:00', color='green')
ax0.axvline('2017-07-02 16:30:00', color='green')
ax0.axvline('2017-07-05 16:30:00', color='green')

ymin, ymax = plt.ylim()
arrowprops = {'width': 1, 'headwidth': 1, 'headlength': 1, 'shrink':0.05 }
ax0.annotate('BigNews1', xy=('2017-07-02 16:30:00', ymax), xytext=(-10, 25), textcoords='offset points',
             rotation=90, va='bottom', ha='center', annotation_clip=False, arrowprops=arrowprops)
ax0.annotate('BlaBlaBla2', xy=('2017-07-02 16:30:00', ymax), xytext=(10, 25), textcoords='offset points',
             rotation=90, va='bottom', ha='center', annotation_clip=False, arrowprops=arrowprops)
ax0.annotate('BigNews3', xy=('2017-07-05 16:30:00', ymax), xytext=(0, 25), textcoords='offset points',
             rotation=90, va='bottom', ha='center', annotation_clip=False, arrowprops=arrowprops)
plt.tight_layout()
plt.show()

这是带有这些批注的顶部外观:

top of the plot

© www.soinside.com 2019 - 2024. All rights reserved.