繁体   English   中英

matplotlib中以交互方式ion()进行的轴编辑将被忽略-范围问题?

[英]Axis edits in interactive mode ion() in matplotlib are ignored - scope problem?

我想根据随时间增长的数据数组绘制图表。 我发现ion()可以重画图,添加新点。 现在,添加一个新点应该会删除一个旧点,并且要实现这一点,我必须添加clf()。 这再次意味着每次绘制时都必须重置轴编辑,但它会忽略依赖于轴手柄的所有修改。 我想知道这是否是由于我调用的函数引起的范围问题? 我是python的新手,如果有比所选方法更直接的方法,也欢迎反馈。

我试图通过不同的功能传递轴手柄,希望这会改变事情,但是没有成功。

import matplotlib.pyplot as plt
import matplotlib.ticker as tck
from time import time

x, y = [], []
counter = 0
plt.ion()
fig, ax1 = plt.subplots()      # ax1 is not used

def axis(ax):
    ax.set_label("Time [s]")
    ax.yaxis.set_major_locator(tck.MultipleLocator(base=0.5))

def plot():
    plt.clf()
    ax = plt.gca()
    axis(ax)
    if len(y) < 3:
        plt.plot(x, y, c='r')
    else:
        plt.plot(x[-3:], y[-3:], c='r')
    plt.draw()
    return ax


for i in range(0,10):
    x.append(time())
    y.append(counter)
    print(i, '\n')
    ax = plot()
    counter +=1
    plt.pause(1)

不需要通过斧头。 用ax1.clear()代替plt.clf()解决了此问题。

暂无
暂无

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

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