5

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')

As a wrap up, I want to put a dot on the function line which indicates that point is the root.

odd
  • 131
  • 1
  • 3
  • 9
  • You cannot use `markevery` to obtain the desired plot, unless you know that `-1,1,2` are actually in `vals`. So first, create a [mcve] as usual when asking a question here. Next, are the values in `vals` or not? Are `vals` and `poly` python lists, sets or numpy arrays? Finally, do you know the positions of those values inside `vals` or not? – ImportanceOfBeingErnest Nov 09 '17 at 21:41
  • Vals and poly are python lists where they represent x and y values of the plot. Vals ranges from -60 to 60. As for the roots question, they are in vals. – odd Nov 09 '17 at 21:56

1 Answers1

9

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()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712