Question

I make numeric simulations and have problems with my plots... My simulation area is 6x35µm with a resolution of 640x1024px. When I want to plot my simulation data using imshow, and set the aspect ratio: aspect=6/35, then the height of the plot is correct but it is much to long? What am I doing wrong?

In the picture:

top: simulation mask, all plots should have the same aspect ratio.

middle: aspect is set to manually set to the aspect of the simulation mask (6/35)

bottom: aspect is set to 1

enter image description here

Here is my code

fig = pl.figure(num=None, figsize=(10, 10))
ax = fig.add_subplot(1,1,1)
img = ax.imshow(data, aspect=6./35)
Was it helpful?

Solution

You also need to account for the difference in resolution along both axis. For your y-axis you have 640 data points for 6 micrometers and for the x-axis 1024 data points for 35 micrometers. Matplotlib assumes both to be equal.

data = np.random.rand(640,1024)

fig, axs = plt.subplots(2,1, figsize=(10, 4))

aspect = 6 / 35

axs[0].set_title('aspect: %1.2f' % aspect)
axs[0].imshow(data, aspect=aspect, interpolation='none')

aspect = (6/35.) * (1024 / 640)

axs[1].set_title('aspect: %1.2f' % aspect)
axs[1].imshow(data, aspect=aspect, interpolation='none')

enter image description here

OTHER TIPS

What you need to do is the following:

img = ax.imshow(data, aspect = 6/35.)

or in other words, make a float of the denominator. 6/35 is equal to zero. 6/35. is not.

Hope that helps

I found the answer somewhere elso in the forum... How can I set the aspect ratio in matplotlib?

def forceAspect(ax,aspect=1):
    im = ax.get_images()
    extent =  im[0].get_extent()
    ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top