TrippinDumplings
TrippinDumplings

Reputation: 1

Plotting with while loop

I have a some x and y coordinates like this:

x = [None, 5, 7, None, None, 9]
y = [1, 2, 3, 4, 5, 6]

I want a while loop to go through each list item in turn and plot a circle marker if it has an x and y coordinate and then the plot must stay there as it continues to plot more values, eventually it will have plotted every value from the lists.

Code:

i = 0
while i < 100:
    plt.plot((b[i]), (a[i]), marker='o')
    plt.ion()
    plt.pause(1)
    i += 1

Some reason, it seems to plot the marker and then it disappears, any ideas?

Upvotes: 0

Views: 2385

Answers (1)

doctorlove
doctorlove

Reputation: 19232

You question gives lists x and y and then uses a and b in the loop, so I'll just make something up for a and b and assume you have the filtering out of None's working.

a = [1, 5, 7, 1, 1, 9]
b = [1, 2, 3, 4, 5, 6]

You just need to set interactive once - not over and over in the loop. But this isn't the cause of the problem - just saying

plt.ion()

for (x, y) in zip(a,b):
    plt.plot(x, y, marker = 'o')
    plt.pause(1)

What I observe is that the points are showing up - but the scale changes to show the new point - others are off the screen. If I zoom out enough all the points are actually there.

You may wish to give some thought to your axes scale; something like

plt.xlim([min(a)-1, max(a)+1])
plt.ylim([min(b)-1, max(b)+1])

should make all your data fit on the same plot without points seeming to disappear

enter image description here

Upvotes: 4

Related Questions