使用matplotlib.pyplot在直线和x轴之间添加垂直虚线

时间:2020-07-01 20:38:34

标签: python matplotlib

我有一个类似这样的情节: enter image description here

我使用注释方法绘制那些箭头

ax.annotate('sample events', (x_value, y_value), xytext=(x_value, -3000), rotation=90, va='top', arrowprops = {'width': 2, 'headwidth': 4, 'linestyle': '--'})

但是我想要的是这样的: enter image description here

如果注释文本在x轴上最好,有什么办法吗?

(我尝试过ax.axvline(x = x_value),但它在整个图上都形成了垂直线)

2 个答案:

答案 0 :(得分:1)

简单的方法是使用function addboldtext() { const doc=DocumentApp.getActiveDocument(); const body=doc.getBody(); const style1={}; style1[DocumentApp.Attribute.BOLD]=true; style1[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';//you can add all of the attributes that you wish body.appendParagraph("This is text").setAttributes(style1) } ,例如:

ax.stem

结果示例图为:

enter image description here

如果要隐藏水平线,请使用import matplotlib.pyplot as plt import numpy as np y = np.random.random(20) fig, ax = plt.subplots() ax.plot(y) ax.stem(y, markerfmt='.') plt.show()

basefmt = " "

答案 1 :(得分:0)

首先:您应避免在代码中使用#,因为它将被解释为comment

根据ax.annotate的{​​{3}},您应轻松指定虚线的线型以及标签位置。 可以找到很好的示例列表documentation

您应该尝试类似的操作:ax.annotate('dashed line', xy=(0.25,0.2), xytext=(0.6,0.2),arrowprops={'arrowstyle': '-', 'ls': 'dashed'}, va='center', rotate=90)