Python 在matplotlib热图中重新标记x轴

Python 在matplotlib热图中重新标记x轴,python,numpy,matplotlib,plot,scipy,Python,Numpy,Matplotlib,Plot,Scipy,我正在制作一张热图。是否可以在X轴上重新标记值,并向其添加常量 例如,我希望使用5、6、7、8、9,而不是x轴上的0、1、2、3、4。您可以在调用imshow时使用关键字参数extent标记x轴和y轴。这里有一些文件 extent : scalars (left, right, bottom, top), optional, default: None Data limits for the axes. The default assigns zero-based row, column in

我正在制作一张热图。是否可以在X轴上重新标记值,并向其添加常量


例如,我希望使用5、6、7、8、9,而不是x轴上的0、1、2、3、4。

您可以在调用
imshow
时使用关键字参数
extent
标记x轴和y轴。这里有一些文件

extent : scalars (left, right, bottom, top), optional, default: None
Data limits for the axes.  The default assigns zero-based row,
column indices to the `x`, `y` centers of the pixels.
根据链接的示例,可以执行以下操作:

from pylab import *
A = rand(5,5)
figure(1)
imshow(A, interpolation='nearest')
grid(True)

left = 4.5
right = 9.5
bottom = 4.5
top = -0.5
extent = [left, right, bottom, top]

figure(2)
imshow(A, interpolation='nearest', extent=extent)
grid(True)

show()

这将仅更改x轴标签。请注意,您必须考虑这样一个事实,即默认值标记像素,而
范围
标记整个轴(因此系数为0.5)。还请注意,
imshow
中y轴的默认标签从上到下增加(从顶部的0增加到底部的4),这意味着我们的
bottom
将大于我们的
top
变量

您可以简单地为循环或列表添加常量,并将其用作新的轴标签,例如:

import matplotlib.pyplot as plt

CONST = 10

x = range(10)
y = range(10)
labels = [i+CONST for i in x]

fig, ax = plt.subplots()

plt.plot(x, y)
plt.xlabel('x-value + 10')

# set custom tick labels
ax.set_xticklabels(labels)

plt.show()


如果有用的话,我将其与其他示例一起添加到我的here中:

如果您只想重新标记当前绘图(单击它以选择它),您可以使用
xticks()
函数(注意
arange()
上限需要比所需的最大值多一个)-例如,从iPython/Python:

xticks(arange(0,5),arange(5,10))
如果要修改python脚本文件,请使用:

plt.xticks(arange(0,5),arange(5,10))

为什么最后是-0.5?我更新了答案来解释。这有意义吗?