python - 将 matplotlib 颜色图集中在特定值上

标签 python python-3.x matplotlib colormap

我正在使用 matplotlib 颜色图“seismic”绘制绘图,并且希望白色以 0 为中心。当我在不进行任何更改的情况下运行脚本时,白色从 0 下降到 -10。我尝试设置 vmin=-50, vmax=50 但在这种情况下我完全失去了白色。关于如何实现这一目标有什么建议吗?

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

设置 vmin、vmax 的绘图:
Plot with vmin, vmax set

最佳答案

您可以手动设置要显示的级别。只要零的左侧和右侧的间隔间距相同,就可以很好地工作。

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

enter image description here

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

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

enter image description here

如果级别不相等,则需要指定vminvmax

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

enter image description here

缺点是您会失去分辨率,因此您可以使用 BoundaryNorm 为不等间距的标签选择等间距的颜色。

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

enter image description here

要更改颜色栏上的刻度标签,以便更改级别以外的其他内容,或者如果它们太严重,您可以使用 ticks 参数。

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

enter image description here

关于python - 将 matplotlib 颜色图集中在特定值上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49282955/

相关文章:

python - 使用带有 graphql 查询的 API 在 R 上抛出 401 错误,但在 Python 上工作正常

python - 使用 apt_pkg 以编程方式安装 debian 软件包时出错

python-3.x - Python上传文件和文件夹到共享点

python - 是否有必要在 python 中显式调用 super().__init__() ?

python - matplotlib 自定义图例中类别的副标题

python - 在caffe安装中安装python包时出错

python - 根据元素是否在列列表中选择 DataFrame 的行

python - 如何修复 "update-alternatives: warning: forcing reinstallation of alternative/usr/bin/python3.8 because link group python3 is broken"?

python - 如何将仅包含字符串的表添加到 matplotlib 图形中

python - 如何使用点绘制 Pandas 数据框的两列