简体   繁体   中英

How to plot incomplete data with matplotlib

I am doing some processing and I want to plot progress. I know my total number of runs, I calculate results on every 100th run and I want to generate a new plot every time. So, my code looks something like this

start = time.time()
speeds = []
for step in range(steps):
    do_my_stuff()
    if step % 100 == 0:
        speeds.append((step + 1) / (time.time() - start))
        x = np.arrange(steps)
        y = np.array(speeds)
        plt.plot(x, y, 'ro')

And, of course, I get "x and y must have same first dimension" error. It seems like such a simple and common issue, but for some reason I cannot find a ready solution. I am probably not googling for the right thing. What am I missing?

Update #1: To make myself a bit more clear. I want to redraw the plot every 100 iterations of my process. I want the plot to reflect the currently accumulated speed points. I want it to scale from 0 to maximum steps and I want it to be empty where speeds were not calculated yet.

I would plot every single pair of x and y interactively, and set the limits of the plot to the desired values. You can still append the data if you need it later on. Look at this example:

import time
import matplotlib.pyplot as plt
import matplotlib

plt.ion()

fig = plt.figure(1,figsize=(5,5))
ax  = fig.add_subplot(111)
steps = 1000
ax.set_xlim(0,steps)
ax.plot(1,1)
plt.draw()

start = time.time()
speeds = []
for step in range(steps):
    #Your stuff here
    if step % 100 == 0:
        speed = (step + 1.0) / (time.time() - start)
        plt.plot(step, speed, 'ko')
        plt.draw()
        plt.pause(1)

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