简体   繁体   English

如何使用Matplotlib绘制不完整的数据

[英]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. 我知道我的总奔跑次数,我每100个奔跑计算一次结果,并且每次都想生成一个新图。 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. 而且,当然,我收到“ x和y必须具有相同的第一维”错误。 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. 更新#1:让自己更加清楚。 I want to redraw the plot every 100 iterations of my process. 我想每隔100次迭代就重画一次绘图。 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. 我希望它从0扩展到最大步长,并且希望它在没有计算速度的地方为空。

I would plot every single pair of x and y interactively, and set the limits of the plot to the desired values. 我将交互地绘制每对x和y,并将图的极限设置为所需的值。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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