Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set_data and autoscale_view matplotlib

I have multiple lines to be drawn on the same axes, and each of them are dynamically updated (I use set_data), The issue being that i am not aware of the x and y limits of each of the lines. And axes.autoscale_view(True,True,True) / axes.set_autoscale_on(True) are not doing what they are supposed to. How do i auto scale my axes?

import matplotlib.pyplot as plt  fig = plt.figure() axes = fig.add_subplot(111)  axes.set_autoscale_on(True) axes.autoscale_view(True,True,True)  l1, = axes.plot([0,0.1,0.2],[1,1.1,1.2]) l2, = axes.plot([0,0.1,0.2],[-0.1,0,0.1])  #plt.show() #shows the auto scaled.  l2.set_data([0,0.1,0.2],[-1,-0.9,-0.8])  #axes.set_ylim([-2,2]) #this works, but i cannot afford to do this.    plt.draw() plt.show() #does not show auto scaled 

I have referred to these already, this , this. In all cases I have come across, the x,y limits are known. I have multiple lines on the axes and their ranges change, keeping track of the ymax for the entire data is not practical

A little bit of exploring got me to this,

xmin,xmax,ymin,ymax = matplotlib.figure.FigureImage.get_extent(FigureImage)  

But here again, i do not know how to access FigureImage from the Figure instance.

Using matplotlib 0.99.3

like image 812
chaitu Avatar asked Aug 25 '11 08:08

chaitu


People also ask

What is XLIM and YLIM in Matplotlib?

xlim() and ylim() to Set Limits of Axes in Matplotlib xlim() and matplotlib. pyplot. ylim() can be used to set or get limits for X-axis and Y-axis respectively. If we pass arguments in these methods, they set the limits for respective axes and if we do not pass any arguments, we get a range of the respective axes.

What is Matplotlib ax ax?

spx.plot(ax=ax,style="k-") This piece of code is calling the plot method for a Series, and inside this method there is an optional argument called 'ax'. The description of this argument says that it is an object of plotting from matplotlib for this plotting you want to do.

What is axes Matplotlib?

Advertisements. Axes object is the region of the image with the data space. A given figure can contain many Axes, but a given Axes object can only be in one Figure. The Axes contains two (or three in the case of 3D) Axis objects.


1 Answers

From the matplotlib docs for autoscale_view:

The data limits are not updated automatically when artist data are changed after the artist has been added to an Axes instance. In that case, use matplotlib.axes.Axes.relim() prior to calling autoscale_view.

So, you'll need to add two lines before your plt.draw() call after the set_data call:

axes.relim() axes.autoscale_view(True,True,True) 
like image 174
John Lyon Avatar answered Sep 23 '22 05:09

John Lyon