0

I am trying to write some code that plots a graph whilst going through a for loop and updates the graph with each iteration using matplotlib. I am plotting the voltage_trim[0:index] in order to avoid the problem of having two lists of different size but I still get an error that x and y must have same first dimensions. Does anyone know how one plots two lists of different size to have an updating real time graph?

voltage_trim = range(16)
current_meas = []


fig1 = plt.figure()


for index, value in enumerate(voltage_trim):

    current_meas.append(random.randint(0,10))

    plt.cla() 
    plt.plot(voltage_trim[:index], current_meas )
marjak
  • 93
  • 7
  • Possible duplicate of [Dynamically updating plot in matplotlib](http://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib) – Vadim Shkaberda Jul 04 '16 at 18:05

1 Answers1

0

Your idea of trimming the x values is good. To make it work use voltage_trim[:index+1].

The index you get from enumerate starts at zero, but for the first returned element you want to already select one x value. Therefore you need to get all the values up to index+1.

Andi
  • 1,233
  • 11
  • 17