如何用我的imshow图设置xticks和yticks?

2024-04-28 00:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码

import numpy as np
import matplotlib.pyplot as plt

with open('nm.dat','r') as f:
    vst = map(float, f)

print vst    

a=np.asarray(vst)
print len(a)

a11=a.reshape(4,22)

plt.imshow(a11, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()

我的形象 enter image description here

我想用等距的0,8,16,24,32,40,48,56,64,72,80,88刻度来标记我的x轴。对于y轴0,2,4,6,8。 如何解决这个问题?


Tags: 代码importnumpymatplotlibaswithnpplt
1条回答
网友
1楼 · 发布于 2024-04-28 00:16:24

您缺少imshow中的extent参数。imshow假设像素和“物理”单元之间存在线性关系。你可以使用:

plt.imshow(a11, cmap='hot', interpolation='nearest', extent=[0,88,0,8], origin='lower')

范围变量必须给定为extent=[xmin,xmax,ymin,ymax]。origin=“lower”参数用于指定必须将[0,0]坐标放置在轴的左下角。否则,它将放置在轴的左上角。

最后,为了只显示某些特定的刻度,您可能需要使用:

ax = plt.gca()
xticks = [0,8,16,24,32,40,48,56,64,72,80,88]
yticks = [0,2,4,6,8]
ax.xaxis.set_xticks(xticks)
ax.xaxis.set_yticks(yticks)

相关问题 更多 >