散景线图:绘制大量线时如何分配颜色

问题描述

我使用for循环(下面的代码)在bokeh中创建了多线图。

输出示例中,只有两条曲线。在这种情况下,我可以为每条曲线设置颜色列表。但是,如果需要绘制大量曲线,如何使用散景调色板之一(例如成对调色板)?我想实现这一点的自动化,以避免每次我增加要绘制的线数时都必须创建颜色列表。

io.Writer

输出

enter image description here

解决方法

正如 Eugene 所建议的,inferno 和 viridis 调色板多达 256 种颜色,通过使用迭代器,您可以自动选择颜色。

import numpy as np
from bokeh.plotting import figure,output_file,show
from bokeh.palettes import inferno# select a palette
import itertools# itertools handles the cycling 

numLines=50

p = figure(width=800,height=400)
x = np.linspace(0,numLines)

colors = itertools.cycle(inferno(numLines))# create a color iterator 

for m in range(numLines):
    y = m * x
    p.line(x,y,color=next(colors))

show(p)

示例调整自When plotting with Bokeh,how do you automatically cycle through a color pallette?

enter image description here