python - 如何在 matplotlib 中以交互模式移动图形窗口?

标签 python matplotlib

我正在尝试使用 matplotlib 监控实时数据。

我发现我可以在 Pyplot 中使用交互模式动态更新绘图。

它运行良好,但一个问题是“我根本无法操作图形窗口”。例如,移动或调整图形窗口的大小。

这是我的代码。

这是交互模式的缺点?或者我使用不当?

import matplotlib.pyplot as plt
import time
import math

# generate data
x = [0.1*_a for _a in range(1000)]
y = map(lambda x : math.sin(x), x)

# interactive mode
plt.ion() # identical plt.interactive(True)

fig, ax = plt.subplots()
# ax = plt.gca()
lines,  = ax.plot([], [])

# ax.set_ylim(-1, 1)
ax.grid()

MAX_N_DATA = 100
x_data = []
y_data = []
for i in range(len(x)):
    # New data received
    x_data.append(x[i])
    y_data.append(y[i])

    # limit data length
    if x_data.__len__() > MAX_N_DATA:
        x_data.pop(0)
        y_data.pop(0)

    # Set Data
    lines.set_xdata(x_data)
    lines.set_ydata(y_data)

    # The data limits are not updated automatically.
    ax.relim()
    # with tight True, graph flows smoothly.
    ax.autoscale_view(tight=True, scalex=True, scaley=True)

    # draw
    plt.draw()
    time.sleep(0.01)

谢谢。

最佳答案

如图this answer to another question , 将 plt.draw() 替换为 plt.pause(0.05)。这为我解决了问题。

关于python - 如何在 matplotlib 中以交互模式移动图形窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699875/

相关文章:

javascript - Scrapy-Selenium 纽约时报问题

python - 如何避免由缩进引起的记录器文件中的额外空格

python - matplotlib 中的自动标记大小

python - jupyter Notebook 上 matplotlib 绘图中的缩放和平移选项

python - 暂停 matplotlib 自定义动画循环

python - 当一列包含特定子字符串时,在多列上剪切子字符串

python - 阴谋冲刺 : Render a Graph only if certain data in a Pandas DataFrame exists

python - 在 Python 中打开 ffmpeg 子进程时出错

python - Matplotlib:3 个图绘制成 2 行,单个图像居中

python - 以微秒分辨率绘制 pandas 数据帧