Plotting a 2D array with matplotlib.imshow


Question

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

TempLake=np.zeros((N+1,Nlayers)) 
TempLake[0]=T0 

Q=np.zeros(N+1) 
Q[0]=0.0 
for i in xrange(N): 
    Q[i+1]=Qn(HSR[i],TD[i],FW[i],TempLake[i][0]) 
    TempLake[i+1]=main_loop(Z,z,Areat0,Areat1,TempLake[i],wind[i],Q[i],Q[i+1]) 


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

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
5
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


Z=np.array((range(1,30),range(31,60),range(61,90))).transpose()

X,Y=np.meshgrid(range(Z.shape[0]+1),range(Z.shape[1]+1))
im = plt.pcolormesh(X,Y,Z.transpose(), cmap='hot')
plt.colorbar(im, orientation='horizontal')
plt.show()

enter image description here

4
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
Icon