Skip to content Skip to sidebar Skip to footer

Imshow - Splitted With Different Pixel Sizes

I try to get the following which should be illustrated in the figure below. Let us assume, for simplicity, I have a numpy array (10x10) which I want to plot with matplotlib imshow.

Solution 1:

This is way simpler if you just use a single axes object. Then also zooming will work flawlessly.

Code:

from matplotlib import pyplot as plt
import numpy as np

# prepare the data
data = np.arange((100))
data = np.reshape(data, (10,10))
data1=data[0:5,:]
data2=data[5:10,:]

# create the figure and a single axis
fig, ax = plt.subplots()

# common arguments to imshow
kwargs = dict(
        origin='lower', interpolation='nearest', vmin=np.amin(data),
        vmax=np.amax(data), aspect='auto')

# draw the data
ax.imshow(data1, extent=[0, 10, 0, 5], **kwargs)
ax.imshow(data2, extent=[0, 10, 5, 7.5], **kwargs)

# optional black line between data1 and data2
ax.axhline(5, color='k')

# set the axis limits
ax.set_ylim(0, 7.5)
ax.set_xlim(0, 10)

# set the xticklabels
xticks = np.arange(0,10)
ax.set_xticks(xticks + 0.5)
ax.set_xticklabels(map(str, xticks))

# set the yticks and labels
yticks = np.concatenate((
        np.arange(0, 5) + 0.5,
        np.arange(5, 7.5, 0.5) + 0.25
        ))
ax.set_yticks(yticks)
ax.set_yticklabels(map(str, xticks))

# show the figure
plt.show()

Result:

enter image description here

Comments:

  • I took the liberty to rename the data1/2 objects in a more intuitive way
  • Thanks to @kazemakase for pointing out the need to adapt the axis ticks.

Post a Comment for "Imshow - Splitted With Different Pixel Sizes"