automatically changing view bounds on resize matlotlib

matplotlib

Solution

One way to do this is to implement a handler for `resize_event`. Here is a short example how this might be done. You can modify it for your needs:

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()

Problem

Is it possible to setup plot to show more data when expanded? Matplotlib plots scale when resized. To show specific area one can use `set_xlim` and such on axes. I have an ecg-like plot showing realtime data, its y limits are predefined, but I want to see more data along x if I expand window or just have big monitor. Im using it in a pyside app and I could just change xlim on resize but I want more clean and generic solution.

Original source