如何更改颜色条轴上的数字

问题描述 投票:0回答:1

我已经制作了一个等值线图并有一个颜色条,但它的标签是随机数,我假设它只是来自数据,我一直在尝试找到一种方法来重新标记它,以便数字从 1 到 12以整数上升。我有什么办法可以做到这一点吗?

我尝试更改限制,但它根本没有改变任何东西。

python matplotlib contour colorbar contourf
1个回答
0
投票

阅读Matplotlib的文档,你应该有答案。让我重现那段代码以适合您的特定情况。

您需要手动调整图形实例上的刻度标签。我对最后两行代码留下了注释,供大家参考。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn


data = np.random.randint(1, 12, size=(4, 4))

fig, ax = plt.subplots()

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')


# this is where the magic happens. 
# I first create the labels on the color map
ticks = np.linspace(1, 12, 12)

# I call in the colorer method on the fig instance previously instantiated and assign ticks
cbar = fig.colorbar(cax, ticks=ticks)
© www.soinside.com 2019 - 2024. All rights reserved.