如何重新标记matplotlib热图的轴刻度 [英] How to relabel axis ticks for a matplotlib heatmap

查看:21
本文介绍了如何重新标记matplotlib热图的轴刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪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热图的轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆