将matplotlib颜色图的中心放在特定值上 [英] Center matplotlib colormap on a specific value

查看:56
本文介绍了将matplotlib颜色图的中心放在特定值上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib颜色图"seismic"进行绘图,并且希望白色以0为中心.运行脚本时,白色没有变化,白色从0降到-10.我尝试然后设置vmin = -50,vmax = 50,但是在那种情况下我完全失去了白色.关于如何实现这一目标的任何建议?

from netCDF4 import Dataset as NetCDFFile导入matplotlib.pyplot作为plt将numpy导入为np从mpl_toolkits.basemap导入底图nc = NetCDFFile('myfile.nc')lat = nc.variables ['lat'] [:]lon = nc.variables ['lon'] [:]时间 = nc.variables['时间'][:]hgt = nc.variables ['hgt'] [:]地图=底图(llcrnrlon = 180.,llcrnrlat = 0.,urcrnrlon = 320.,urcrnrlat = 80.)lons,lats = np.meshgrid(lon,lat)x,y = map(lons,lats)cs = map.contourf(x,y,hgt [0],cmap ='地震')cbar = plt.colorbar(cs,orientation='horizo​​ntal',shrink=0.5,cmap =地震")cbar.set_label('500mb 位势高度异常(m)')map.drawcoastlines()map.drawparallels(np.arange(20,80,20),labels=[1,1,0,0], linewidth=0.5)map.drawmeridians(np.arange(200,320,20),labels=[0,0,0,1], linewidth=0.5)plt.show()`

或者,如果您希望颜色条与数据成比例,

fig.colorbar(cont,orientation="horizo​​ntal",spacing="proportional")

如果级别不相等,则需要指定 vmin vmax .

levels = [-50,-40,-30,-20,-10,10,30,50,80,100]cont = ax.contourf(X,Y,Z,levels,cmap ="seismic",vmin = -50,vmax = 50)

缺点是分辨率降低,因此您可以使用BoundaryNorm为间距不相等的标签选择间距相等的颜色.

 将matplotlib.pyplot导入为plt导入 matplotlib.colors将numpy导入为npx = np.linspace(-6.3,6.3)y = np.linspace(-3.1,3.1)X,Y = np.网格(x,y)Z = -np.cos(X)* np.cos(Y)* 45等级= [-50,-40,-30,-20,-10,10,30,50,80,100]规范 = matplotlib.colors.BoundaryNorm(levels, len(levels)-1)无花果,ax = plt.subplots(figsize =(4,2))cont = ax.contourf(X,Y,Z,levels,cmap=plt.get_cmap("seismic",len(levels)-1), norm=norm)fig.colorbar(cont,orientation="horizo​​ntal")plt.show()

要更改颜色栏上的刻度标签,以便更改级别以外的其他内容,或者如果它们过于复杂,您可以使用 ticks 参数.

 将matplotlib.pyplot导入为plt将numpy导入为npx = np.linspace(-6.3,6.3)y = np.linspace(-3.1,3.1)X,Y = np.网格(x,y)Z = -np.cos(X)* np.cos(Y)* 45级别 = np.arange(-45,50,5)等级=等级[等级!= 0]ticks = np.arange(-40,50,10)图, ax = plt.subplots(figsize=(4,2))cont = ax.contourf(X,Y,Z,levels,cmap =地震",间隔=比例")fig.colorbar(cont,orientation="horizo​​ntal",ticks=ticks,spacing="proportional")plt.show()

I'm making plots using matplotlib colormap "seismic" and would like to have the white color centered on 0. When I run my script with no changes, white falls from 0 to -10. I tried then setting vmin=-50, vmax=50 but I completely lose the white in that case. Any suggestions on how to accomplish that?

from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
nc = NetCDFFile('myfile.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
hgt = nc.variables['hgt'][:]
map = Basemap(llcrnrlon=180.,llcrnrlat=0.,urcrnrlon=320.,urcrnrlat=80.)
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)
cs = map.contourf(x,y,hgt[0],cmap='seismic')
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5, 
cmap='seismic')
cbar.set_label('500mb Geopotential Height Anomalies(m)')
map.drawcoastlines()
map.drawparallels(np.arange(20,80,20),labels=[1,1,0,0], linewidth=0.5)
map.drawmeridians(np.arange(200,320,20),labels=[0,0,0,1], linewidth=0.5)
plt.show()`

Plot with defaults

Plot with vmin, vmax set

解决方案

You can set the levels you want to show manually. As long as you have the same spacing of intervals to the left and to the right of zero this works nicely.

levels = [-50,-40,-30,-20,-10,10,20,30,40,50]
ax.contourf(X,Y,Z, levels)

Example:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45

levels = [-50,-40,-30,-20,-10,10,20,30,40,50]
fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels, cmap="seismic")
fig.colorbar(cont, orientation="horizontal")
plt.show()

Or, if you want the colorbar to be proportional to the data,

fig.colorbar(cont, orientation="horizontal", spacing="proportional")

If levels are unequal, you need to specify vmin and vmax.

levels = [-50,-40,-30,-20,-10,10,30,50,80,100]
cont = ax.contourf(X,Y,Z,levels, cmap="seismic", vmin=-50, vmax=50)

The disadvantage is that you loose resolution, hence you may use a BoundaryNorm to select equally spaced colors for unequally spaced labels.

import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np

x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45

levels = [-50,-40,-30,-20,-10,10,30,50,80,100]
norm = matplotlib.colors.BoundaryNorm(levels, len(levels)-1)
fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels,cmap=plt.get_cmap("seismic",len(levels)-1), norm=norm)
fig.colorbar(cont, orientation="horizontal")
plt.show()

To change the ticklabels on the colorbar so something other than the levels or in case they are too dence you may use the ticks argument.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-6.3,6.3)
y = np.linspace(-3.1,3.1)
X,Y = np.meshgrid(x,y)
Z = -np.cos(X)*np.cos(Y)*45

levels = np.arange(-45,50,5)
levels = levels[levels!=0]
ticks=np.arange(-40,50,10)

fig, ax = plt.subplots(figsize=(4,2))
cont = ax.contourf(X,Y,Z,levels,cmap="seismic", spacing="proportional")
fig.colorbar(cont, orientation="horizontal", ticks=ticks, spacing="proportional")
plt.show()

这篇关于将matplotlib颜色图的中心放在特定值上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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