【问题标题】:Moving x-axis in matplotlib during real time plot (python)在实时绘图期间在 matplotlib 中移动 x 轴(python)
【发布时间】:2017-01-01 14:46:08
【问题描述】:

我想在实时绘图期间操纵 x 轴,以便一次最多看到 10 个样本。 似乎 plt.axis() 在绘图初始化后只更新一次。有什么建议?提前致谢!

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])

【问题讨论】:

    标签: python matplotlib plot real-time axis


    【解决方案1】:

    您必须在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])
    

    这就是诀窍! :)

    说明:您只向两个参数添加了 10 一次。最后你总是把 10 加到 0 和 10 上,只剩下一个更新。

    【讨论】:

    • 不客气。顺便说一句,在最小的工作示例上做得很好,我们看到的还不够多!
    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 2016-02-01
    • 2020-09-18
    • 2020-06-09
    • 2019-04-17
    • 2012-03-26
    相关资源
    最近更新 更多