Plotting a 2D array with matplotlib.imshow


The np.array that results from this loop has 4383 rows and 6 columns. I have tried without success to use pylab.imshow() from matplotlib(pylab) to display the array. The objective is to creat an image of the array, in wich the colors gradient represent the magnitude of the array values. Each row of the array represents the variation in depth of a lake temperature in each day (4383 days). Thus the objective is to find diferences in lake temperatures in depth and with time. Thank you


for i in xrange(N): 

im = plt.imshow(tem, cmap='hot') 
plt.colorbar(im, orientation='horizontal') 

This is the result: The legend is fine, but the x-axis are inverted and the image doesn´t appear enter image description here

This is what I need: enter image description here

1/28/2016 6:18:59 PM

Accepted Answer

You need to use pcolor or pcolormesh instead of imshow. This is because in imshow the aspect of figure is same as the array, which in your case is 4383x6.

import pylab as plt
import numpy as np


im = plt.pcolormesh(X,Y,Z.transpose(), cmap='hot')
plt.colorbar(im, orientation='horizontal')

enter image description here

8/2/2012 12:04:12 PM

You can use imshow if you just set the aspect when you call it. As follows:

im = plt.imshow(tem, cmap='hot', aspect=aspect_ratio*(cols/rows)) 

where aspect_ratio here would set the actual aspect ratio you want and cols/rows just normalizes the original aspect ratio to 1. cols and rows are the numbers of columns and rows (e.g. rows = data.shape[0], cols = data.shape[1]).

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow