简体   繁体   English

如何在Matplotlib图形中标记特定数据点

[英]How to mark specific data points in matplotlib graph

I have a plot that I want to mark some values of x on the graph like in the following image, (ps I put the dots by hand) see the plot 我有一个图,我想在图形上标记x的某些值,如下图所示,(ps我用手把点放在上面) 看图

I tried the following code, yet it did not work as I have expected. 我尝试了以下代码,但并未按预期工作。

roots = [-1,1,2]
plt.plot(vals,poly,markevery=roots,label='some graph')

I guess something wrong with the image I tried to post above; 我想我上面发布的图片有问题; as a wrap up, I want to put a dot, on the function line which indicates that point is the root. 作为总结,我想在功能线上放一个圆点,指示该点是根。

Assuming that the vals are integers in the range of [-60,60] , one would need to find the positions of [-1,1,2] in that list and use those positions as the argument to markevery . 假设vals在范围内的整数[-60,60]一个需要找到的位置[-1,1,2]在该列表中,并使用这些位置作为参数markevery

import matplotlib.pyplot as plt

vals,poly = range(-60,60), range(-60,60)

plt.plot(vals, poly, label='some graph')
roots = [-1,1,2]

mark = [vals.index(i) for i in roots]
print(mark)
plt.plot(vals,poly,markevery=mark, ls="", marker="o", label="points")

plt.show()

Alternatively, you could also just plot only those values, 另外,您也可以只绘制那些值,

import matplotlib.pyplot as plt

vals,poly = range(-60,60), range(-60,60)

plt.plot(vals, poly, label='some graph')
roots = [-1,1,2]

mark = [vals.index(i) for i in roots]

plt.plot(roots,[poly[i] for i in mark], ls="", marker="o", label="points")

plt.show()

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

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