Setting axes to a constant scale

Hi,

I have started to use pyplot to create graphs, saving the images and
generating an animation from the collection. Pyplot autoscale the
axes, which isn't suitable for an animation. At the moment I reset
them (for every frame) using the xlim / ylim functions:

import matplotlib.pyplot as plt

...

def draw(self):
    self.fig = plt.figure()
    self.ax = self.fig.add_subplot(1,1,1)

    for e in self.graph.E:
        x, y = [e.u.x, e.v.x], [e.u.y, e.v.y]
        plt.plot(x, y, 'b', zorder=2, lw=3)

    for v in self.graph.V:
        x, y = [v.x, v.x], [v.y, v.y]
        plt.scatter(x, y, c='g', zorder=1, s=120)

    # XXX: There should be a better way than this?
    plt.xlim( (0, 100) )
    plt.ylim( (0, 100) )

Is there a better way to do this?

Thanks,
Blake

Hi Blake,

the following addition should do what you want:

self.ax = self.fig.add_subplot(1,1,1, autoscale_on=False, xlim=(0, 100),
ylim=(0, 100))

or by hand after creation of the axes

self.ax.set_autoscale_on(False)
xlim(...) # or self.ax.set_xlim(...)
ylim(...)

That is you switch off matplotlibs autoscaling and set proper x/y limits
initially. With that you can remove all further xlim/ylim calls.

···

On Tuesday 03 March 2009 11:55:49 Blake Friedman wrote:

Hi,

I have started to use pyplot to create graphs, saving the images and
generating an animation from the collection. Pyplot autoscale the
axes, which isn't suitable for an animation. At the moment I reset
them (for every frame) using the xlim / ylim functions:

import matplotlib.pyplot as plt

...

def draw(self):
    self.fig = plt.figure()
    self.ax = self.fig.add_subplot(1,1,1)

    for e in self.graph.E:
        x, y = [e.u.x, e.v.x], [e.u.y, e.v.y]
        plt.plot(x, y, 'b', zorder=2, lw=3)

    for v in self.graph.V:
        x, y = [v.x, v.x], [v.y, v.y]
        plt.scatter(x, y, c='g', zorder=1, s=120)

    # XXX: There should be a better way than this?
    plt.xlim( (0, 100) )
    plt.ylim( (0, 100) )

Is there a better way to do this?

Thanks,
Blake