Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: limits when using plot and imshow in same axes

Tags:

I've been trying to plot an ellipse into an imshow plot. It works, but plotting the ellipse after plotting the image seems to increase xlim and ylim, resulting in a border, which I'd like to get rid of:

Note that there is NO white border directly after calling imshow only.

My code looks as follows:

self.dpi = 100 self.fig = Figure((6.0, 6.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setMinimumSize(800, 400) self.cax = None self.axes = self.fig.add_subplot(111) self.axes.imshow(channel1, interpolation="nearest") self.canvas.draw() self.axes.plot(dat[0], dat[1], "b-") 

I've tried setting the limits before and after calling "plot", with no effect

# get limits after calling imshow xlim, ylim = pylab.xlim(), pylab.ylim() ... # set limits before/after calling plot self.axes.set_xlim(xlim) self.axes.set_ylim(ylim) 

How can I force plot not to increase existing figure limits?

Solution (thanks to Joe):

#for newer matplotlib versions self.axes.imshow(channel1, interpolation="nearest") self.axes.autoscale(False) self.axes.plot(dat[0], dat[1], "b-")  #for older matplotlib versions (worked for me using 0.99.1.1) self.axes.imshow(channel1, interpolation="nearest") self.axes.plot(dat[0], dat[1], "b-", scalex=False, scaley=False) 
like image 523
soramimo Avatar asked Feb 02 '12 22:02

soramimo


People also ask

What are the two ways to adjust axis limits of the plot using matplotlib?

Adjust axis limits: To set the limits of x and y axes, we use the commands plt. xlim() and plt. ylim().

Can a matplotlib figure have more than one set of axes?

Create multiple y axes with a shared x axis. This is done by creating a twinx axes, turning all spines but the right one invisible and offset its position using set_position . Note that this approach uses matplotlib.

What are the methods to adjust axis limits matplotlib?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

Does Imshow normalize?

By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).


1 Answers

What's happening is that the axis is autoscaling to match the extents of each item you plot. Images are autoscaled much tighter than lines, etc (imshow basically calls ax.axis('image')).

Getting the axis limits before and setting them after should have worked. (It's cleaner to just do limits = axes.axis() before and axes.axis(limits) after, though.)

However, if you don't want things to autoscale, it's best to just turn autoscaling off after the initial plot. Try axes.autoscale(False) after plotting the image.

As an example, compare this:

import matplotlib.pyplot as plt import numpy as np  fig, ax = plt.subplots() ax.imshow(np.random.random((10,10))) ax.plot(range(11)) plt.show() 

enter image description here


With this:

import matplotlib.pyplot as plt import numpy as np  fig, ax = plt.subplots() ax.imshow(np.random.random((10,10))) ax.autoscale(False) ax.plot(range(11)) plt.show() 

enter image description here

like image 145
Joe Kington Avatar answered Sep 23 '22 17:09

Joe Kington