59

I'm plotting data in Python using matplotlib. I am updating the data of the plot based upon some calculations and want the ylim and xlim to be rescaled automatically. Instead what happens is the scale is set based upon the limits of the initial plot. A MWE is

import random
import matplotlib.pyplot as pyplot

pyplot.ion()

x = range(10)
y = lambda m: [m*random.random() for i in range(10)]

pLine, = pyplot.plot(x, y(1))

for i in range(10):
    pLine.set_ydata(y(i+1))
    pyplot.draw()

The first plot command generates a plot from [0,1] and I can see everything just fine. At the end, the y-data array goes from [0,10) with most of it greater than 1, but the y-limits of the figure remain [0,1].

I know I can manually change the limits using pyplot.ylim(...), but I don't know what to change them to. In the for loop, can I tell pyplot to scale the limits as if it was the first time being plotted?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
jlconlin
  • 14,206
  • 22
  • 72
  • 105
  • 2
    See these two related answers: http://stackoverflow.com/a/3215060/623518 and http://stackoverflow.com/a/7198623/623518 – Chris Jun 11 '12 at 19:05

1 Answers1

79

You will need to update the axes' dataLim, then subsequently update the axes' viewLim based on the dataLim. The approrpiate methods are axes.relim() and ax.autoscale_view() method. Your example then looks like:

import random
import matplotlib.pyplot as pyplot

pyplot.ion()

x = range(10)
y = lambda m: [m*random.random() for i in range(10)]

pLine, = pyplot.plot(x, y(1))

for i in range(10):
    pLine.set_ydata(y(i+1))

ax = pyplot.gca()

# recompute the ax.dataLim
ax.relim()
# update ax.viewLim using the new dataLim
ax.autoscale_view()
pyplot.draw()
taras
  • 6,566
  • 10
  • 39
  • 50
pelson
  • 21,252
  • 4
  • 92
  • 99
  • 33
    Note that if you have used `set_xlim()` on the same axes before, `autoscale_view()` will NOT work. In this case, `autoscale()` should be used instead (tested on 1.5.x). – kawing-chiu Feb 24 '16 at 07:00
  • 2
    It seems, `relim()` and `autoscale*()` only take into account positions of markers and ignore the error bars. There must be a way to compute the limits taking into account error bars (or, in general, all drawn elements on a plot) since by default they are drawn correctly. If one 'zooms' a plot using `ylim` (or `set_ylim`), then I was not able to find a way to 'unzoom' back to the original view. – S.V Feb 01 '19 at 13:50
  • This works pretty well! There is one thing I would like to ask. If using with `ax.errorbar(x, y, error)`, this method is not taking the error bar into account and thus error bars can potentially be hidden after the axis. Is there a way to make resolve this issue? Thanks! – zyy Apr 30 '20 at 04:38
  • 2
    @zyy Not an answer, just a note: I think the reason for this is that the errorbar uses Collections, which are ignored by ax.relim(): see the [docu](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.relim.html#matplotlib.axes.Axes.relim). – normanius Jan 04 '21 at 01:29