我已经制作了一个等值线图并有一个颜色条,但它的标签是随机数,我假设它只是来自数据,我一直在尝试找到一种方法来重新标记它,以便数字从 1 到 12以整数上升。我有什么办法可以做到这一点吗?
我尝试更改限制,但它根本没有改变任何东西。
阅读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)