Autoscaling imagesc() plot and imshow() plots

Matlab/Octave axis ratio

Matlab/Octave default axis scaling scales x and y axes proportionally to the axes values. If one axis values span a much wider range than the other axis, the smaller span axis gets very thin and almost invisible.

Fix invisible axis in plot

insert axis('square') after imagesc()

rdat = rand(1000,10);
imagesc(rdat)

axis('square')

Force axes to represent actual data size

This is the opposite to the previous problem, where you want the axes to be representative of number of data elements (skinny images appear skinny)–insert axis('equal') after imagesc()

rdat = rand(1000,10);
imagesc(rdat)

axis('equal')

Matplotlib axis ratio

In Matplotlib, the same issue occurs, which can be overcome with aspect='auto' property of ax.imshow()

You can conversely allow the “true” axes ratio driven by the actual data aspect='equal'

You can set this after the plot is created in axes ax by

#!/usr/bin/env python
import numpy as np
from matplotlib.pyplot import figure,show

rdat = np.random.rand(1000,10)

ax = figure().gca()
ax.imshow(rdat)

# %% pick ONE of the following

# non-shared axes
ax.set_aspect('equal','box') 

# shared axes only (e.g. subplots(sharex=True))
ax.set_aspect('equal','box-forced')  

Leave a Comment