python - matplotlib 中交互模式 ion() 中的 Axis 编辑被忽略 - 范围问题?

标签 python axis python-interactive

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

我尝试通过不同的函数传递 Axis 句柄,希望这会改变事情,但没有成功。

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() 解决了该问题。

关于python - matplotlib 中交互模式 ion() 中的 Axis 编辑被忽略 - 范围问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637777/

相关文章:

python - 模块函数 vs 静态方法 vs 类方法 vs 无装饰器 : Which idiom is more pythonic?

python - 数据框到 CSV

java - Axis 展开误差

python - 反转图像的 y Axis 而不翻转图像

python - 在 matplotlib 中交互式添加和删除散点

python - Django Makemessages CommandError ASCII 编码

python - 为什么我的 Python 代码从一个函数跳转到另一个我没有调用的函数?

r - axis.break和ggplot2或gap.plot?情节可能太复杂了

python - 等同于普通 python 中的 Ipython 运行命令

python - 是否可以让 Python Interactive Interpreter 在加载时运行脚本?