Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically changing view bounds on resize matlotlib

Tags:

matplotlib

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.

like image 240
vpoverennov Avatar asked Nov 11 '12 06:11

vpoverennov


People also ask

How do I increase Figsize in Matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.

How do you avoid overlapping plots in python?

Dot Size. You can try to decrease marker size in your plot. This way they won't overlap and the patterns will be clearer.

What does PLT Tight_layout () do?

tight_layout automatically adjusts subplot params so that the subplot(s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.


1 Answers

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()
like image 181
btel Avatar answered Nov 27 '22 01:11

btel