Imshow: degree and aspect

I am writing a software system that visualizes slices and forecasts through a 3D data set. I use matplotlib and in particular imshow to render image buffers that I return from my analysis code.

Since I would like to annotate the images using the axes of the graph, I use the degree keyword, which imshow allows to match the pixel coordinates of the image buffer in the coordinate system of the data space.

Unfortunately, matplotlib does not know about units. Say (taking an artificial example) that I want to build an image with dimensions of 1000 m X 1 km . In this case, the degree will be something like [0, 1000, 0, 1] . Despite the fact that the image array is square, since the aspect ratio implied by a keyword of degree 1000, the resulting axis of the graph also has an aspect ratio of 1000.

Is it possible to force the aspect ratio of the chart while preserving automatically generated main marks and marks that I get using the degree keyword?

+58
python matplotlib imshow
Nov 14 '12 at 18:09
source share
1 answer

You can do this by manually setting the aspect of the image (or by automatically zooming in).

By default, imshow sets the aspect of the graph to 1, as this is often what people want for image data.

In your case, you can do something like:

 import matplotlib.pyplot as plt import numpy as np grid = np.random.random((10,10)) fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(6,10)) ax1.imshow(grid, extent=[0,100,0,1]) ax1.set_title('Default') ax2.imshow(grid, extent=[0,100,0,1], aspect='auto') ax2.set_title('Auto-scaled Aspect') ax3.imshow(grid, extent=[0,100,0,1], aspect=100) ax3.set_title('Manually Set Aspect') plt.tight_layout() plt.show() 

enter image description here

+98
Nov 15 '12 at 2:36
source share



All Articles