Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the scale of imshow in matplotlib without stretching the image?

I wanted to plot using imshow in a manner similar to the second example here http://www.scipy.org/Plotting_Tutorial but to redefine the scale for the axis. I'd also like the image to stay still while I do this!

The code from the example:

from scipy import *
from pylab import *

# Creating the grid of coordinates x,y 
x,y = ogrid[-1.:1.:.01, -1.:1.:.01]

z = 3*y*(3*x**2-y**2)/4 + .5*cos(6*pi * sqrt(x**2 +y**2) + arctan2(x,y))

hold(True)
# Creating image
imshow(z, origin='lower', extent=[-1,1,-1,1])

xlabel('x')
ylabel('y')
title('A spiral !')

# Adding a line plot slicing the z matrix just for fun. 
plot(x[:], z[50, :])

show()

If I modify the extent to be wider, eg:

imshow(z, origin='lower', extent=[-4,4,-1,1])

Then the resulting image is stretched. But all I wanted to do was change the ticks to coincide with my data. I know I can use pcolor to conserve the X and Y data, though that has other ramifications to it.

I found this answer which allows me to manually redo all the ticks:

How do I convert (or scale) axis values and redefine the tick frequency in matplotlib?

But that seems a bit overkill.

Is there a way to only change the extent shown by the labels?

like image 971
ubershmekel Avatar asked Jun 10 '12 13:06

ubershmekel


People also ask

How do you normalize Imshow?

Just specify vmin=0, vmax=1 . 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).

What is Imshow interpolation PLT?

This example displays the difference between interpolation methods for imshow . If interpolation is None, it defaults to the rcParams["image. interpolation"] (default: 'antialiased' ). If the interpolation is 'none' , then no interpolation is performed for the Agg, ps and pdf backends.

How do I increase the size of an image in Matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.


Video Answer


1 Answers

A help(imshow) will find the aspect argument, which after a bit of experimentation seems to give what you want (a square image of the spiral but with x scale from -4 to 4 and y from -1 to 1) when used like this:

imshow(z, origin='lower', extent=[-4,4,-1,1], aspect=4)

But now your plot is still from -1 to 1, so you'd have to modify that as well...

plot(x[:]*4, z[50, :])

I think when you have several elements that would have to be modified, just using a one-line tick relabeling instead isn't overkill:

xticks(xticks()[0], [str(t*4) for t in xticks()[0]])
like image 103
weronika Avatar answered Oct 31 '22 00:10

weronika