0

I have a range of lists with same len, as below:

a=[10,56,78,90]
b=[2,8,33,10]

And I might need to include a new list, for example.

I have been using the following to plot lists a and b:

plt.plot(x,a)
plt.plot(x,b)

As you can see, I want to use the same x-axis for both of them.

However, I'd like to figure out how this piece of code should be in order to receive new lists or drop a current list dynamically.

Any thoughts on this?

tks

2 Answers 2

1

One easy way is to run a loop that plots each list separately and it looks like its moving. To do that, you have to put your lists in a list. For example:

l=[a,b];
for i in range(0,len(l):
   plt.plot(x,l[i]);

you can add title, legend axis, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's how:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

#setup the figure 
plt.figure(figsize=(12,4))
x=[1,2,3,4]
initial=4*[0]
line1 = plt.plot(x, initial, 'k', lw=1.5, label=0)
plt.title('Dynamic Plotting', fontsize=16)
plt.ylabel('Dynamic List', fontsize=12)
plt.xlabel('x', fontsize=12)

plt.ion()   # set interactive mode
plt.show()


a=[10,56,78,90]
b=[2,8,33,10]
u=[a,b]

#loop over your list
for i,list in enumerate(u):
    for l in line1:
        l.remove()
        del l

    line1 = plt.plot(x, list, 'k', lw=1.5, label=i)
    plt.legend()
    plt.gcf().canvas.draw()
    plt.pause(2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.