在resize matlotlib上自动更改视图边界

min*_*ess 5 matplotlib

是否可以设置绘图以在展开时显示更多数据?

Matplotlib在调整大小时绘制比例.要显示可以set_xlim在轴上使用的特定区域.我有一个类似ecg的情节显示实时数据,其y限制是预定义的,但我希望看到更多的数据沿着x,如果我扩展窗口或只是有大显示器.

我在pyside应用程序中使用它,我可以在调整大小时更改xlim,但我想要更干净和通用的解决方案.

bte*_*tel 7

一种方法是为它实现一个处理程序resize_event.以下是如何完成此操作的简短示例.您可以根据需要进行修改:

import numpy as np
import matplotlib.pyplot as plt

def onresize(event):
    width = event.width
    scale_factor = 100.
    data_range = width/scale_factor
    start, end = plt.xlim()
    new_end = start+data_range
    plt.xlim((start, new_end))

if __name__ == "__main__":

    fig = plt.figure()
    ax = fig.add_subplot(111)
    t = np.arange(100)
    y = np.random.rand(100)
    ax.plot(t,y)
    plt.xlim((0, 10))
    cid = fig.canvas.mpl_connect('resize_event', onresize)
    plt.show()
Run Code Online (Sandbox Code Playgroud)