简体   繁体   中英

Matplotlib error: x and y do not have the same first dimension

I have read multiple threads already on the subject, but I did not find proper solutions.

I am using the following method to draw my animated figure in a while loop:

        ax1.plot(x_vals, y_vals, 'ro')
        ax2.plot(x_vals_1, y_vals_1, 'bo')
        fig.canvas.draw()

y_vals is list that gets appended when I read data from a one data source, and x_vals is a list that appends an increment to the sample count ie

y_vals = [1,2,3] x_vals = [1,2,3]

y_vals_1 and x_vals_1 is the same concept. All 4 of these are global variables, which means that they are updated whenever the data comes in and it gets plotted right away. I am suspecting that there could be a time that y_vals gets updated before x_vals gets updated and then this while loop is trying to plot them with a mismatch in dimension therefor I get the following error: x and y do not have the same first dimension.

Is there a way to create my x_vals right before y_vals which is very fast? (y_vals can get very large in size). Or, is there a way to only feed y_vals to a matplotlib chart and it will know that the x_vals is just a sample count?

If x_vals is nothing more than a (zero-based) count, you can omit it from the call to plot() :

ax1.plot(y_vals, 'ro')

...or you could generate it on-the-fly if it has to be one-based:

ax1.plot(range(1, len(y_vals)+1), y_vals, 'ro')

In Python 3, range() itself is efficient (it doesn't allocate space for all len(y_vals) values—it just records the start, stop and step-size values) but I don't know whether that efficiency holds throughout the internals of the plot() implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM