gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:36:16 39 4
gpt4 key购买 nike

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

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

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

%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

但是,这个包不知何故在日期时间格式方面存在问题。对于这里显示的这个错误示例代码,我真的很抱歉,但我真的是一个初学者,我希望有人能帮助解决我的问题。

最佳答案

这是一些显示 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

关于Python matplotlib - 如何为垂直线添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654759/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com