Ste*_*ell 19 python plot bokeh
我想使用循环来加载和/或修改数据,并使用Bokeh在循环中绘制结果(我熟悉Matplotlib的axes.color_cycle).这是一个简单的例子
import numpy as np
from bokeh.plotting import figure, output_file, show
output_file('bokeh_cycle_colors.html')
p = figure(width=400, height=400)
x = np.linspace(0, 10)
for m in xrange(10):
y = m * x
p.line(x, y, legend='m = {}'.format(m))
p.legend.location='top_left'
show(p)
Run Code Online (Sandbox Code Playgroud)
这会产生这个情节

如何使颜色循环而不编码颜色列表和模数操作,以便在颜色数量用完时重复?
有一些关于GitHub的讨论,问题351和2201,但目前尚不清楚如何使这项工作.搜索的时候,我得到了四支安打文档为cycle color实际上没有包含这个词cycle的页面上的任何地方.
Ell*_*iot 20
最简单的方法是获取颜色列表并使用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)
Run Code Online (Sandbox Code Playgroud)
您可以定义一个简单的生成器来为您循环颜色。
在python 3中:
from bokeh.palettes import Category10
import itertools
def color_gen():
yield from itertools.cycle(Category10[10])
color = color_gen()
Run Code Online (Sandbox Code Playgroud)
或在 python 2(或 3)中:
from bokeh.palettes import Category10
import itertools
def color_gen():
for c in itertools.cycle(Category10[10]):
yield c
color = color_gen()
Run Code Online (Sandbox Code Playgroud)
当您需要新颜色时,请执行以下操作:
p.line(x, y1, line_width=2, color=color)
p.line(x, y2, line_width=2, color=color)
Run Code Online (Sandbox Code Playgroud)
这是上面的例子:
p = figure(width=400, height=400)
x = np.linspace(0, 10)
for m, c in zip(range(10), color):
y = m * x
p.line(x, y, legend='m = {}'.format(m), color=c)
p.legend.location='top_left'
show(p)
Run Code Online (Sandbox Code Playgroud)
小智 7
两个小的更改将使Python 3的先前答案工作.
改变: for m, color in zip(range(10), colors):
之前: for m, color in itertools.izip(xrange(10), colors):