python输入参数改变图形,更改matplotlib imshow()图形轴上的值

本文介绍如何使用matplotlib.pyplot.imshow展示二维数据,并提供了一个实例,展示了如何通过定义网格和xticks/yticks来正确标记坐标轴,从而避免默认的像素计数标记。这种方法适用于快速且准确地呈现网格数据,比pcolor等工具更高效。

我也遇到过类似的问题,谷歌把我发到了这个帖子上。我的解决方案有点不同,不那么紧凑,但希望这对某人有用。

使用matplotlib.pyplot.imshow显示图像通常是显示二维数据的快速方法。但是,默认情况下,这会使用像素计数标记轴。如果要打印的二维数据对应于由数组x和y定义的某个统一网格,则可以使用matplotlib.pyplot.xticks和matplotlib.pyplot.yticks使用这些数组中的值标记x和y轴。这些标签将与轴上的像素计数相关联,对应于实际的网格数据。这样做比使用pcolor之类的工具要快得多。

以下是对您的数据的尝试:import matplotlib.pyplot as plt

# ... define 2D array hist as you did

plt.imshow(hist, cmap='Reds')

x = np.arange(80,122,2) # the grid to which your data corresponds

nx = x.shape[0]

no_labels = 7 # how many labels to see on axis x

step_x = int(nx / (no_labels - 1)) # step between consecutive labels

x_positions = np.arange(0,nx,step_x) # pixel count at label position

x_labels = x[::step_x] # labels you want to see

plt.xticks(x_positions, x_labels)

# in principle you can do the same for y, but it is not necessary in your case

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值