shell
shell

Reputation: 1937

matplotlib plotting a dot every nth x

I'm trying to plot two things:

The first is this:

plotted_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I then want to plot a dot every nth value in the plotted values list. Say n is 5:

nth_value_plot = [5, 10]

I want to plot the nth_value_plot values such that the 5 and 10 share the same x, y coordinate as the 5 and 10 in the plotted_values. Plotting as such plots the nth_value_plot with the x coordinates 0 and 1 as expected:

plt.plot(plotted_values)
plt.plot(nth_value_plot, "o")
plt.show()

How do I correctly plot this as described above?

EDIT:

The final plot coordinates (x, y) should be:

plotted_values = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]

nth_value_plot = [[4, 5], [9, 10]]

The current plot from the code above has plotted values with these coordinates and nth_value_plot with the coordinates:

nth_value_plot = [[0, 5], [1, 10]]

Thanks

Upvotes: 3

Views: 24896

Answers (3)

Frank Mobley
Frank Mobley

Reputation: 31

Within the plot command there is a 'markevery' command that permits you to set the number of points to skip within the data array.

Upvotes: 3

Lee
Lee

Reputation: 31050

Assuming your x values are the same as your y values:

v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n=5
plt.scatter(v[::n],v[::n])

enter image description here

Upvotes: 15

Ionut Hulub
Ionut Hulub

Reputation: 6326

You must specify the x coordinates:

Replace

plt.plot(nth_value_plot, "o")

with

n = 5
plt.plot([x + n - 1 for x in range(0, len(plotted_values), n)], nth_value_plot, "o")

Upvotes: 5

Related Questions