用"contourf"设置颜色栏范围.在matplotlib中 [英] Set Colorbar range with "contourf" in matplotlib

查看:48
本文介绍了用"contourf"设置颜色栏范围.在matplotlib中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与contourf一起使用时如何减少颜色条限制?可以使用"vmin"和"vmax"很好地设置图形本身的颜色边界,但不会修改颜色栏的边界.

 将numpy导入为np导入matplotlib.pyplot作为pltx = np.arange(20)y = np.arange(20)数据= x [:,None] + y [None ,:]X,Y = np.网格(x,y)vmin = 0vmax = 15#我的尝试fig,ax = plt.subplots()Silhouettef_ = ax.contourf(X,Y,data,400,vmin = vmin,vmax = vmax)cbar = fig.colorbar(contourf_)cbar.set_clim(vmin,vmax) 

 #使用https://stackoverflow.com/questions/53641644/set-colorbar-range-with-contourf中的解决方案等级= np.linspace(vmin,vmax,400 + 1)fig,ax = plt.subplots()Silhouettef_ = ax.contourf(X,Y,data,level = levels,vmin = vmin,vmax = vmax)cbar = fig.colorbar(contourf_)plt.show() 

"

"

请求的解决方案:

How to reduce the colorbar limit when used with contourf ? The color bound from the graphs itself are well set with "vmin" and "vmax", but the colorbar bounds are not modified.

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(20)
y = np.arange(20)
data = x[:,None]+y[None,:]

X,Y = np.meshgrid(x,y)
vmin = 0
vmax = 15

#My attempt
fig,ax = plt.subplots()
contourf_ = ax.contourf(X,Y,data, 400, vmin=vmin, vmax=vmax)
cbar = fig.colorbar(contourf_)
cbar.set_clim( vmin, vmax )

# With solution from https://stackoverflow.com/questions/53641644/set-colorbar-range-with-contourf
levels = np.linspace(vmin, vmax, 400+1)
fig,ax = plt.subplots()
contourf_ = ax.contourf(X,Y,data, levels=levels, vmin=vmin, vmax=vmax)
cbar = fig.colorbar(contourf_)
plt.show()

solution from "Set Colorbar Range in matplotlib" works for pcolormesh, but not for contourf. The result I want looks like the following, but using contourf.

fig,ax = plt.subplots()
contourf_ = ax.pcolormesh(X,Y,data[1:,1:], vmin=vmin, vmax=vmax)
cbar = fig.colorbar(contourf_)

Solution from "set colorbar range with contourf" would be ok if the limit were extended, but not if they are reduced.

I am using matplotlib 3.0.2

解决方案

The following always produces a bar with colours that correspond to the colours in the graph, but shows no colours for values outside of the [vmin,vmax] range.

It can be edited (see inline comment) to give you exactly the result you want, but that the colours of the bar then still correspond to the colours in the graph, is only due to the specific colour map that's used (I think):

# Start copied from your attempt
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(20)
y = np.arange(20)
data = x[:, None] + y[None, :]

X, Y = np.meshgrid(x, y)
vmin = 0
vmax = 15


fig, ax = plt.subplots()

# Start of solution
from matplotlib.cm import ScalarMappable
levels = 400

level_boundaries = np.linspace(vmin, vmax, levels + 1)

quadcontourset = ax.contourf(
    X, Y, data,
    level_boundaries,  # change this to `levels` to get the result that you want
    vmin=vmin, vmax=vmax
)


fig.colorbar(
    ScalarMappable(norm=quadcontourset.norm, cmap=quadcontourset.cmap),
    ticks=range(vmin, vmax+5, 5),
    boundaries=level_boundaries,
    values=(level_boundaries[:-1] + level_boundaries[1:]) / 2,
)

Always correct solution that can't handle values outside [vmin,vmax]:

Requested solution:

这篇关于用"contourf"设置颜色栏范围.在matplotlib中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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