问题描述
我有一个图,我想在图上标记 x 的一些值,如下图所示,(p.s. 我手工放置了点)看剧情
I have a plot that I want to mark some values of x on the graph like in the following image, (p.s. I put the dots by hand)see the plot
我尝试了以下代码,但没有按预期工作.
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.
推荐答案
假设 vals
是 [-60,60]
范围内的整数,人们会需要在该列表中找到 [-1,1,2]
的位置,并将这些位置用作 markevery
的参数.
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
.
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()
这篇关于如何在matplotlib图中标记特定数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!