python - 设置 matplotlib colorbar 范围(比绘制的值范围更大)

标签 python matplotlib colorbar

我想绘制一些数据(0 到 18 之间),但我希望颜色栏显示 0 到 40 之间的范围。 在看了几个在线示例之后,我不知道如何实现我需要的东西!

这是一个简单的例子:

import matplotlib.pyplot as plt
import numpy as np

rd = np.random.rand(40,100) # random samples from a uniform distribution over [0, 1]
surface = 18 * rd           # maximum value will be 18

fig = plt.figure()
ax = fig.add_subplot(111)
cores = ax.contourf(surface[:], vmin=0, vmax=40)

cbar = plt.colorbar(cores)

我也试过这个:

cbar = plt.colorbar(cores, extend='both', extendrect=True)
cbar.set_clim(0, 40.)

但我一直得到相同的图像,颜色条从 0 到 20 不等! 我知道我可以使用 set_over 方法,但我不想获得单一颜色...我想要我的整个颜色范围(如 cores 中所定义,从0 到 40) 出现!

enter image description here

感谢您的帮助!

最佳答案

终于解决了。 我必须传递一个关键字来控制要绘制的级别,而不是 vminvmax,如下所示:

import matplotlib.pyplot as plt
import numpy as np

rd = np.random.rand(40,100)
surface = 18 * rd           # maximum value will be 18

fig = plt.figure()
ax = fig.add_subplot(111)

cores = ax.contourf(surface[:], levels=range(41))
cbar = plt.colorbar(cores)

plt.show()

我得到了我想要的图像: enter image description here

关于python - 设置 matplotlib colorbar 范围(比绘制的值范围更大),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723538/

相关文章:

python - 选择超过 1 列不是 NaN 的 Pandas 行

python - 在给定日期对 Python 系列或数据帧重新采样

python - 如何将 matplotlib (python) 窗口保留在后台?

python - Pandas 系列计数的适当功能

image - Matlab - 缩放图像的颜色条

python - Matplotlib : quiver and imshow superimposed, 如何设置两个颜色条?

python - 配置 matplotlib colorbar 以匹配 3D 表面值

python - 什么是 "runtime context"?

python - 创建内存中 zip 文件并作为 http 响应返回的函数

python - 在 matplotlib 中仅绘制一种 rgb 颜色