python设置色条

1、最简单的“三步走”:

from matplotlib import cm
import matplotlib.colors
import csv
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import matplotlib as mpl
from matplotlib import colors


cmap = mpl.colormaps.get_cmap('jet')
norm1 = mpl.colors.Normalize(vmin=n0, vmax=n1)
im1 = mpl.cm.ScalarMappable(norm=norm1, cmap=cmap)
fig, ax = plt.subplots()
cbar = plt.colorbar(im1, ax=ax)
plt.show()

2、更改默认色条中某部分的颜色:

# 额外设置前几个颜色
viridis = cm.get_cmap('jet', 256)
newcolors = viridis(np.linspace(0, 1, 256))

white = np.array([255/255, 255/255, 255/255, 1])
newcolors[:9, :] = white    # 设置前9个颜色为白色
blue1 = np.array([176/255, 226/255, 255/255, 1])
newcolors[8:10, :] = blue1   # 设置为蓝色

cmap = ListedColormap(newcolors)
norm1 = mpl.colors.Normalize(vmin=0, vmax=10)
im1 = mpl.cm.ScalarMappable(norm=norm1, cmap=cmap)

fig, ax = plt.subplots()
cbar = plt.colorbar(im1, ax=ax)
plt.show()

3、自定义色条:

colors_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF']
# 创建自定义颜色映射
cmap = colors.LinearSegmentedColormap.from_list('custom_cmap', colors_list)
norm1 = mpl.colors.Normalize(vmin=0, vmax=20)
im1 = mpl.cm.ScalarMappable(norm=norm1, cmap=cmap)

fig, ax = plt.subplots()
cbar = plt.colorbar(im1, ax=ax)
plt.show()

你可能感兴趣的:(python气象可视化,python,开发语言,matplotlib,pycharm,经验分享)