简体   繁体   English

在matplotlib.pyplot中使用虚线标记y值

[英]Marking y value using dotted line in matplotlib.pyplot

I am trying to plot a graph using matplotlib.pyplot . 我正在尝试使用matplotlib.pyplot绘制图形。

import matplotlib.pyplot as plt
import numpy as np

x = [i for i in range (1,201)]
y = np.loadtxt('final_fscore.txt', dtype=np.float128)
plt.plot(x, y, lw=2)
plt.show()

It looks something like this: 看起来像这样: 到目前为止我做了什么

I want to mark the first value of x where y has reached the highest ( which is already known, say for x= 23, y= y[23]), like this figure shown below: 我想标记x的第一个值,其中y已达到最高(已知,例如x = 23,y = y [23]),如下图所示:

我想做的事

I have been searching this for some time now, with little success. 我已经搜索了一段时间,但收效甚微。 I have tried adding a straight line for now, which is not behaving the desired way: 我已经尝试过添加一条直线,这不符合预期的方式:

import matplotlib.pyplot as plt
import numpy as np

x = [i for i in range (1,201)]
y = np.loadtxt('final_fscore.txt', dtype=np.float128)

plt.plot(x, y, lw=2)

plt.plot([23,y[23]], [23,0])

plt.show()

Resulting graph: 结果图:

不是我想要的

Note: I want to make the figure like in the second graph. 注意:我想使图像第二张图一样。

It's not clear what y[23] would do here. 尚不清楚y[23]在这里会做什么。 You would need to find out the maximum value and the index at which this occurs ( np.argmax ). 您将需要找出最大值和发生该最大值的索引( np.argmax )。 You may then use this to plot a 3 point line with those coordinates. 然后,您可以使用它绘制带有这些坐标的3点线。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(9)

x = np.arange(200)
y = np.cumsum(np.random.randn(200))
plt.plot(x, y, lw=2)

amax = np.argmax(y)
xlim,ylim = plt.xlim(), plt.ylim()
plt.plot([x[amax], x[amax], xlim[0]], [xlim[0], y[amax], y[amax]],
          linestyle="--")
plt.xlim(xlim)
plt.ylim(ylim)

plt.show()

在此处输入图片说明

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

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