设置matplotlib彩条范围(范围大于绘制的值) [英] Setting matplotlib colorbar range (larger range than the values plotted)

查看:78
本文介绍了设置matplotlib彩条范围(范围大于绘制的值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些数据(0到18之间),但我希望颜色条显示0到40之间的范围.在浏览了几个在线示例之后,我对如何实现所需的目标一无所知!

I want to plot some data (between 0 and 18) but I want the colorbar to show a range between 0 and 40. After going through several online examples, I have no idea on how to achieve what I need!

这是一个简单的例子:

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)出现!

But I keep getting the same image, with a colorbar ranging from 0 to 20! I know I could use the set_over method, but I don't want to get a single color... I want my whole color range (as defined in cores, from 0 to 40) to appear!

感谢您的帮助!

推荐答案

我终于设法解决了它.我必须传递一个关键字来控制绘制级别,而不是 vmin vmax

I finally managed to solve it. Instead of vmin and vmax, I must pass a keyword to control the levels to draw, like this:

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()

然后我得到了想要的图像:

And I get the image I wanted:

这篇关于设置matplotlib彩条范围(范围大于绘制的值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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