怎么重新标记matplotlib热图的轴刻度

本教程将介绍如何重新标记matplotlib热图的轴刻度的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

怎么重新标记matplotlib热图的轴刻度 教程 第1张

问题描述

我正在跟踪this example以生成热图。是否有可能重新标记X轴上的值,并为其添加一个常量。

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

推荐答案

您可以通过在调用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.

完成链接的示例后,您可以执行以下操作

import matplotlib.pyplot as plt
import numpy as np

A = np.random.rand(5, 5)
plt.figure(1)
plt.imshow(A, interpolation='nearest')
plt.grid(True)

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

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

plt.show()

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

好了关于怎么重新标记matplotlib热图的轴刻度的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。