When plotting with Bokeh, how do you automatically cycle through a color pallette?

后端 未结 4 1568
别那么骄傲
别那么骄傲 2021-01-01 10:40

I want to use a loop to load and/or modify data and plot the result within the loop using Bokeh (I am familiar with Matplotlib\'s axes.color_cycle). Here is a simple exampl

4条回答
  •  悲&欢浪女
    2021-01-01 11:03

    It is probably easiest to just get the list of colors and cycle it yourself using itertools:

    import numpy as np
    from bokeh.plotting import figure, output_file, show
    
    # select a palette
    from bokeh.palettes import Dark2_5 as palette
    # itertools handles the cycling
    import itertools  
    
    output_file('bokeh_cycle_colors.html')
    
    p = figure(width=400, height=400)
    x = np.linspace(0, 10)
    
    # create a color iterator
    colors = itertools.cycle(palette)    
    
    for m, color in zip(range(10), colors):
        y = m * x
        p.line(x, y, legend='m = {}'.format(m), color=color)
    
    p.legend.location='top_left'
    show(p)
    

提交回复
热议问题