如何在Python的Bokeh绘图中扩展有限的调色板
如何在Python的Bokeh绘图中扩展有限的调色板?
针对你的需求,这里有几种实用的方法来扩展Bokeh中的有限调色板,让颜色过渡更平滑,适配更多的数据值:
方法1:直接使用Bokeh预定义的长调色板
Bokeh内置的很多调色板都提供了不同长度的版本,比如你用到的Reds9(9种颜色),还有Reds256(256种颜色),这类长调色板本身就是平滑的渐变,直接替换即可:
from bokeh.plotting import figure, show, output_file from bokeh.sampledata.iris import flowers from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar from bokeh.palettes import Reds256 # 使用256色的版本 p = figure(title = "Iris Morphology") p.xaxis.axis_label = "Petal Length" p.yaxis.axis_label = "Petal Width" source = ColumnDataSource(flowers) # 反转调色板(如果需要) Reds256.reverse() # 创建颜色映射器,指定数据的最小和最大值 exp_cmap = LinearColorMapper( palette=Reds256, low=flowers['sepal_length'].min(), high=flowers['sepal_length'].max() ) # 绘制散点图,用sepal_length映射颜色 p.circle( x='petal_length', y='petal_width', color={'field': 'sepal_length', 'transform': exp_cmap}, source=source, size=10, alpha=0.8 ) # 添加颜色条 color_bar = ColorBar( color_mapper=exp_cmap, label_standoff=12, location=(0,0), title='Sepal Length' ) p.add_layout(color_bar, 'right') output_file('iris_morphology.html') show(p)
方法2:从现有短调色板扩展出更多颜色
如果你只有一个短调色板(比如自定义的或者Reds9),可以用linear_palette函数来生成任意数量的过渡颜色,实现平滑渐变:
from bokeh.plotting import figure, show, output_file from bokeh.sampledata.iris import flowers from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar from bokeh.palettes import Reds9, linear_palette p = figure(title = "Iris Morphology") p.xaxis.axis_label = "Petal Length" p.yaxis.axis_label = "Petal Width" source = ColumnDataSource(flowers) # 反转原始调色板(不修改原列表的话可以用 reversed_reds = Reds9[::-1]) Reds9.reverse() # 把Reds9扩展成100种颜色的平滑渐变 extended_palette = linear_palette(Reds9, 100) exp_cmap = LinearColorMapper( palette=extended_palette, low=flowers['sepal_length'].min(), high=flowers['sepal_length'].max() ) p.circle( x='petal_length', y='petal_width', color={'field': 'sepal_length', 'transform': exp_cmap}, source=source, size=10, alpha=0.8 ) color_bar = ColorBar( color_mapper=exp_cmap, label_standoff=12, location=(0,0), title='Sepal Length' ) p.add_layout(color_bar, 'right') output_file('iris_morphology.html') show(p)
方法3:利用LinearColorMapper自动插值
其实你不需要手动扩展调色板,LinearColorMapper本身会自动在离散调色板的颜色之间进行插值,生成连续的渐变效果。只要正确设置low和high参数,就能让颜色平滑过渡:
from bokeh.plotting import figure, show, output_file from bokeh.sampledata.iris import flowers from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar from bokeh.palettes import Reds9 p = figure(title = "Iris Morphology") p.xaxis.axis_label = "Petal Length" p.yaxis.axis_label = "Petal Width" source = ColumnDataSource(flowers) # 反转调色板 Reds9.reverse() # 直接用Reds9,LinearColorMapper会自动插值颜色 exp_cmap = LinearColorMapper( palette=Reds9, low=flowers['sepal_length'].min(), high=flowers['sepal_length'].max() ) p.circle( x='petal_length', y='petal_width', color={'field': 'sepal_length', 'transform': exp_cmap}, source=source, size=10, alpha=0.8 ) color_bar = ColorBar( color_mapper=exp_cmap, label_standoff=12, location=(0,0), title='Sepal Length' ) p.add_layout(color_bar, 'right') output_file('iris_morphology.html') show(p)
小提示:
- 反转调色板时,
Reds9.reverse()会直接修改原列表,如果你不想破坏原始调色板,可以创建副本:reversed_reds = Reds9[::-1] - 一定要确保
low和high参数对应你要映射的数据字段的极值,这样颜色映射才会准确贴合数据分布
内容的提问来源于stack exchange,提问作者littleworth
相关产品推荐
相关产品推荐

