Skip to content Skip to sidebar Skip to footer

Pcolor Data Plot In Python

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

Solution 1:

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

enter image description here

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 inrange(NumY):
    for j inrange(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 inrange(NumY-1):
    for j inrange(NumX-1):
        plt.text(j,i, Matrix[i,j], color="w")
plt.colorbar()

plt.show()

Post a Comment for "Pcolor Data Plot In Python"