5

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

4 Answers 4

9

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]).

Sign up to request clarification or add additional context in comments.

1 Comment

If you don't want to do the calculation by hand you can also use aspect="auto" which does the job for you, trying to fit the image to the figure.
5

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

Comments

1

You can use the axis function from matplotlib.pyplot:

axis('auto')

So your exemple would become :

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.axis('auto')
plt.show() 

1 Comment

Can you give me an exammple, I can´t find this function
1

Maybe I'm wrong but for the you can still use imshow just transposing the image

im = plt.imshow(tem.transpose(),cmap='hot',origin='lower',aspect='auto')

with lower saying that the plost start from the bottom left, and auto keyword in imshow. But as I said maybe I do not understand the problem

1 Comment

No, in imshow the aspect of figure is array dependent, so transpose will just rotate it by 90 degree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.