# Overlaying part of an matrix image on another.

Hi

I have a array, M, which is (4Nx4M), and an array (image), im, which is NxM.
I can currently plot the matrix as a 2d image using imshow using:

import matplotlib.pyplot as plt
from matplotlib import cm

# some code for reading in the matrix

cmap = cm.get_cmap(‘jet’, 256)
imM = plt.imshow(M, cmap=cmap, vmin= -1, vmax=1)

But now i would like to plot im on top of M, such that it covers the firs element of M.
If I do

plt.hold()
plt.imshow(im)

I only see im, and not M. I’m used to doing this in Matlab, where this would work.

Can anyone explain me what I’m doing wrong?

Kind Regards

A call to `plt.autoscale` should fix your problem. It looks like `imshow` rescales the axes limits to the current image limits, instead of the limits for all the data in the axes. (Executable example below; note, axes “hold” by default, so it’s not necessary to call hold).

-Tony

import numpy as np
import matplotlib.pyplot as plt

background = np.random.uniform(0, 255, size=(20, 20))
overlay = np.arange(25).reshape((5, 5))

plt.imshow(background, interpolation=‘nearest’, cmap=plt.cm.gray)

plt.imshow(overlay, cmap=plt.cm.jet, alpha=0.5)

# You could also replace this with `plt.axis([0, 20, 0, 20])

plt.autoscale()
plt.show()

···

On Fri, Jan 27, 2012 at 9:13 AM, Pål Gunnar Ellingsen <paalge@…287…> wrote:

Hi

I have a array, M, which is (4Nx4M), and an array (image), im, which is NxM.
I can currently plot the matrix as a 2d image using imshow using:

import matplotlib.pyplot as plt
from matplotlib import cm

# some code for reading in the matrix

cmap = cm.get_cmap(‘jet’, 256)
imM = plt.imshow(M, cmap=cmap, vmin= -1, vmax=1)

But now i would like to plot im on top of M, such that it covers the firs element of M.

If I do

plt.hold()

plt.imshow(im)

I only see im, and not M. I’m used to doing this in Matlab, where this would work.

Can anyone explain me what I’m doing wrong?

Kind Regards

Hi

Thank you very much, it worked perfectly Kind regards

Pål

···

On 27 January 2012 15:29, Tony Yu <tsyu80@…1003…7…> wrote:

On Fri, Jan 27, 2012 at 9:13 AM, Pål Gunnar Ellingsen <paalge@…287…> wrote:

Hi

I have a array, M, which is (4Nx4M), and an array (image), im, which is NxM.
I can currently plot the matrix as a 2d image using imshow using:

import matplotlib.pyplot as plt
from matplotlib import cm

# some code for reading in the matrix

cmap = cm.get_cmap(‘jet’, 256)
imM = plt.imshow(M, cmap=cmap, vmin= -1, vmax=1)

But now i would like to plot im on top of M, such that it covers the firs element of M.

If I do

plt.hold()

plt.imshow(im)

I only see im, and not M. I’m used to doing this in Matlab, where this would work.

Can anyone explain me what I’m doing wrong?

Kind Regards

A call to `plt.autoscale` should fix your problem. It looks like `imshow` rescales the axes limits to the current image limits, instead of the limits for all the data in the axes. (Executable example below; note, axes “hold” by default, so it’s not necessary to call hold).

-Tony

import numpy as np
import matplotlib.pyplot as plt

background = np.random.uniform(0, 255, size=(20, 20))
overlay = np.arange(25).reshape((5, 5))

plt.imshow(background, interpolation=‘nearest’, cmap=plt.cm.gray)

plt.imshow(overlay, cmap=plt.cm.jet, alpha=0.5)

plt.autoscale()
plt.show()