Matplotlib imshow() 的手动定义轴标签 [英] Manually-defined axis labels for Matplotlib imshow()

查看:29
本文介绍了Matplotlib imshow() 的手动定义轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 100, size=(10, 10))
plt.imshow(data, cmap='jet', interpolation='nearest')
plt.show()

给出下图:

但是,我想手动定义它们,而不是对应于数组中索引的轴标签.例如,我希望轴标签不是上面的 (0, 2, 4, 6, 8),而是 (0, 10, 20, 30 ...).

However, instead of the axis labels corresponding to the index in the array, I want to manually define them. For example, instead of the axis labels being (0, 2, 4, 6, 8) as above, I want them to be (0, 10, 20, 30 ...).

这是我为此尝试过的代码:

Here is the code I have tried for this:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 100, size=(10, 10))
labels = range(0, 100, 10)
plt.imshow(data, cmap='jet', interpolation='nearest')
plt.xticks(labels)
plt.yticks(labels)
plt.show()

然而,这给出了下图:

如何获得具有第一个外观的图形但具有像第二个外观的轴标签的图形呢?

How can I achieve a figure with an appearance like the first one, but with axis labels like the second one?

推荐答案

最好的方法是修改imshow参数中的范围值:

The best way to do this is to modify the extent values in argument of your imshow :

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randint(0, 100, size=(10, 10))
plt.imshow(data, cmap='jet', interpolation='nearest', extent=[0, 100, 0, 100])
plt.show()

这篇关于Matplotlib imshow() 的手动定义轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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