Pcolor data plot in Python

两盒软妹~` 提交于 2019-12-12 02:15:02

问题


I'm trying to plot a matrix in python using pcolor. This is my code but it's not working. can you show me how to plot the matrix?!

Matrix = np.zeros((NumX, NumY))

for i in range(NumX):
    for j in range(NumY):
        Matrix[i][j] = Data[i*NumY+j+1]


# Set up a regular grid of interpolation points
xi = np.arange(0, NumX*1.5, 1.5)
yi = np.arange(0, NumY*1.5, 1.5)
X, Y = np.meshgrid(xi, yi)
intensity = np.array(Matrix)

plt.pcolormesh(X, Y, Matrix)
plt.colorbar()
plt.show()

this is the error :

TypeError: Dimensions of C (22, 30) are incompatible with X (22) and/or Y (30); see help(pcolormesh)


回答1:


You need to mind the indexing rules for arrays. X is the second dimension, Y is the first dimension.

import numpy as np; np.random.seed(1) 
import matplotlib.pyplot as plt

NumX, NumY = 5,7
Data = np.random.randint(1,9,size=NumX*NumY+1)

Matrix = np.zeros((NumY, NumX))

for i in range(NumY):
    for j in range(NumX):
        Matrix[i,j] = Data[i*NumX+j+1]

print(Matrix)

xi = np.arange(0, NumX)
yi = np.arange(0, NumY)
X, Y = np.meshgrid(xi, yi)

plt.pcolormesh(X, Y, Matrix)
for i in range(NumY-1):
    for j in range(NumX-1):
        plt.text(j,i, Matrix[i,j], color="w")
plt.colorbar()

plt.show()


来源:https://stackoverflow.com/questions/42687454/pcolor-data-plot-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!