【问题标题】:update matplotlib figure dynamically动态更新 matplotlib 图
【发布时间】:2020-12-28 19:05:22
【问题描述】:

我正在使用 matplotlib 绘制一些图形并动态更新它们 为此,我使用列出的代码中所示的 set_data 方法
问题: 当我需要为每一帧突出显示绘图上的某个点时,旧点仍在绘图上

#display pics
fig,ax = plt.subplots(2,4)

im3 = ax[0][3].plot(x,y)[0]

#################################################
#       some image processing code              #
#################################################

plt.ion()
for pic_index in range(no_of_pics):

    im3.set_ydata(smoothed_histogram)
    #im3.set_data(threshold,smoothed_histogram[threshold],'g*')
    ax[0][3].plot(threshold,smoothed_histogram[threshold],'r*')
    ax[0][3].plot(effective_threshold,smoothed_histogram[effective_threshold],'r*')
    
    plt.pause(0.01)
    input("Press any key to continue...")

下图所示问题

应该只有两个红点,每个循环都更新

我该如何克服这个问题?

【问题讨论】:

  • 尝试添加一个 plt.cla() 来清除当前轴
  • 我是这样做的 ax[0][3].cla() ax[0][3].plot(range(0,256),smoothed_histogram)[0] ax[0][3 ].plot(threshold,smoothed_histogram[threshold],'r*') ax[0][3].plot(effective_threshold,smoothed_histogram[effective_threshold],'r*')

标签: python matplotlib


【解决方案1】:

你更新了smoothed_histogramydata,你可以用你的* 标记做同样的事情:

import numpy as np
import matplotlib.pyplot as plt

n = 100
x = np.arange(n)
y = np.sort(np.random.random(n))

fig, ax = plt.subplots()
ax.plot(x, y)
h_marker = ax.plot(x[0], y[0], marker='o', color='r')[0]

for i in range(n):
    h_marker.set_data(x[i], y[i])
    plt.pause(0.01)

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 2012-06-12
    • 1970-01-01
    • 2022-01-25
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多