python中color(),Python中的Pcolor数据图

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)

解决方案

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

2207b339d2c0d07418c8c634e548c13d.png

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值