mgilson
mgilson

Reputation: 309929

matplotlib optionally autoscale axis range on the left or right

How do I programmatically set the range of a matplotlib plot, optionally keeping autscaling (on either the left or the right):

import numpy as np
import matplotlib.pyplot as plt

a = np.arange(10)

fig = plt.figure()
ax = fig.add_subplot(111)
myrange = None,None
ax.set_xlim(myrange)
ax.plot(a-5,a)
plt.show()

This sets the range to 0 to 1 instead of my desired -5 to 4 (autoscaling). I've also tried:

 ax.set_xlim(*myrange)

But that doesn't do any better. I know about the auto=True option, but that doesn't seem to work with left and right keyword arguments. In other words, I'd also like to be able to set the range on the left to autoscale (-5) and to limit it on the right to a particular value -- e.g. : myrange = None,3


For those who might be familiar with gnuplot, I'm interested in the equivalent of gnuplot's:

set xrange [*:max]

or

set xrange [max:*]

syntax.

Upvotes: 1

Views: 824

Answers (1)

Holger
Holger

Reputation: 2175

You have to call ax.set_xlim after ax.plot.

import numpy as np
import matplotlib.pyplot as plt

a = np.arange(10)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(a-5,a)
ax.set_xlim(xmin = -1.)
plt.show()

Upvotes: 1

Related Questions