简体   繁体   English

使用matplotlib.imshow绘制2D数组

[英]Plotting a 2D array with matplotlib.imshow

The np.array that results from this loop has 4383 rows and 6 columns. 由此循环产生的np.array有4383行和6列。 I have tried without success to use pylab.imshow() from matplotlib(pylab) to display the array. 我试过没有成功使用matplotlib(pylab)中的pylab.imshow() )来显示数组。 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). 阵列的每一行代表每天湖泊温度的深度变化(4383天)。 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 结果如下:图例很好,但x轴反转,图像不显示 在此输入图像描述

This is what I need: 这就是我需要的: 在此输入图像描述

You can use imshow if you just set the aspect when you call it. 如果只是在调用时设置方面,则可以使用imshow 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 (eg rows = data.shape[0] , cols = data.shape[1] ). 其中aspect_ratio将设置您想要的实际宽高比, cols/rows只是将原始宽高比标准化为1. colsrows是列和行的数量(例如rows = data.shape[0]cols = data.shape[1] )。

You need to use pcolor or pcolormesh instead of imshow . 您需要使用pcolorpcolormesh而不是imshow This is because in imshow the aspect of figure is same as the array, which in your case is 4383x6. 这是因为在imshow中,图的方面与数组相同,在您的情况下是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()

在此输入图像描述

You can use the axis function from matplotlib.pyplot: 您可以使用matplotlib.pyplot中的axis函数:

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() 

Maybe I'm wrong but for the you can still use imshow just transposing the image 也许我错了,但是你仍然可以使用imshow只是转置图像

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. 较低的说法是从左下角开始,而在imshow中使用auto关键字。 But as I said maybe I do not understand the problem 但正如我所说,也许我不明白这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM