简体   繁体   English

在实时绘图(python)中在matplotlib中移动x轴

[英]Moving x-axis in matplotlib during real time plot (python)

I want to manipulate the x-axis during a real time plot so that at most a number of 10 samples are seen at a time. 我想在实时绘图中操纵x轴,这样一次最多可以看到10个样本。 It seems like plt.axis() updates just once after the plot has been initialized. 在初始化绘图之后,似乎plt.axis()仅更新一次。 Any suggestions? 有什么建议么? Thanks in advance! 提前致谢!

import numpy as np
import matplotlib.pyplot as plt

# Initialize
x_axis_start = 0
x_axis_end = 10

plt.axis([x_axis_start, x_axis_end, 0, 1])
plt.ion()

# Realtime plot
for i in range(100):
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.10)
    # print(i)

    if i%10 == 0 and i>1:
        # print("Axis should update now!")
        plt.axis([x_axis_start+10, x_axis_end+10, 0, 1])

You have to update x_axist_start and x_axis_end in the if statement! 您必须在if语句中更新x_axist_startx_axis_end

if i%10 == 0 and i>1:
    print("Axis should update now!")
    x_axis_start += 10
    x_axis_end += 10
    plt.axis([x_axis_start, x_axis_end, 0, 1])

This does the trick! 这就是诀窍! :) :)

Explanation: You only added 10 once to both parameters. 说明:您只向两个参数添加了一次。 In the end you always added 10 to 0 and 10, leaving you with only one update. 最后,您总是添加10到0和10,只留下一个更新。

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

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