You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matplotlib中使用颜色列表绘制连续的自定义colorbar

Matplotlib自定义连续渐变colorbar解决方案

你当前生成离散色条的核心原因是使用了面向离散色阶的ListedColormap类,且指定了N=4参数限制色阶数量为4,无法生成连续过渡效果。

要实现白色到橙色的连续渐变,需要改用matplotlib.colors.LinearSegmentedColormap类的from_list方法生成线性渐变色图,修改后的代码如下:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.image as image
# 导入线性渐变色图类
from matplotlib.colors import LinearSegmentedColormap

# 生成从白色到橙色的连续渐变色图,默认256个色阶足以实现平滑过渡效果
cmap = LinearSegmentedColormap.from_list('custom_wht_to_orange', ['white', 'orange'])
map_int = ax3.imshow(integrated, extent=(60, 70, 23.5, 25.0), cmap = cmap, zorder=-1, aspect="auto")
cbar_int = fig1.colorbar(map_int, ax=ax3, fraction=0.046, pad=0.04 )
cbar_int.set_label('Ramp', size=15, rotation=90)
cbar_int.ax.tick_params(labelsize=15) 
map_int.set_clim(1, 20) #colorbar_limit
plt.show()

可选调整项:

  • 如果你需要调整过渡平滑度,可以在from_list方法中增加第三个参数指定色阶数量,比如N=1024可以得到更细腻的渐变效果
  • 如果需要多色连续渐变,直接在颜色列表中按顺序添加颜色即可,比如['white', 'yellow', 'orange']会生成白→黄→橙的连续过渡效果

内容的提问来源于stack exchange,提问作者Muddasir Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:06:02