使用 Bokeh 绘图时,如何自动循环调色板?
When plotting with Bokeh, how do you automatically cycle through a color pallette?
我想使用循环加载 and/or 修改数据并使用 Bokeh 在循环内绘制结果(我熟悉 Matplotlib's 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)
生成此图
如何使颜色循环而不编写颜色列表和模数运算以在颜色数量用完时重复?
GitHub 上有一些与此相关的讨论,cycle color
的问题 351 and 2201, but it is not clear how to make this work. The four hits I got when searching the documentation 实际上并没有在页面的任何地方包含单词 cycle
。
获取颜色列表并使用 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)
两个小改动将使之前的答案对 Python 3 有效。
已更改:for m, color in zip(range(10), colors):
之前:for m, color in itertools.izip(xrange(10), colors):
您可以定义一个简单的生成器来为您循环颜色。
在python 3:
from bokeh.palettes import Category10
import itertools
def color_gen():
yield from itertools.cycle(Category10[10])
color = color_gen()
或 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()
当您需要新颜色时,请执行以下操作:
p.line(x, y1, line_width=2, color=color)
p.line(x, y2, line_width=2, color=color)
这是上面的例子:
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)
在 Python > 3.7 中你可以这样做:
from bokeh.palettes import Category10_10
color = Category10_10.__iter__()
p.line(x, y1, line_width=2, color=next(color))
这将循环遍历列表的每个元素,直到您每次使用 next()
。
python 中的每个序列类型都可以 return 一个迭代器对象。
我想使用循环加载 and/or 修改数据并使用 Bokeh 在循环内绘制结果(我熟悉 Matplotlib's 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)
生成此图
如何使颜色循环而不编写颜色列表和模数运算以在颜色数量用完时重复?
GitHub 上有一些与此相关的讨论,cycle color
的问题 351 and 2201, but it is not clear how to make this work. The four hits I got when searching the documentation 实际上并没有在页面的任何地方包含单词 cycle
。
获取颜色列表并使用 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)
两个小改动将使之前的答案对 Python 3 有效。
已更改:
for m, color in zip(range(10), colors):
之前:
for m, color in itertools.izip(xrange(10), colors):
您可以定义一个简单的生成器来为您循环颜色。
在python 3:
from bokeh.palettes import Category10
import itertools
def color_gen():
yield from itertools.cycle(Category10[10])
color = color_gen()
或 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()
当您需要新颜色时,请执行以下操作:
p.line(x, y1, line_width=2, color=color)
p.line(x, y2, line_width=2, color=color)
这是上面的例子:
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)
在 Python > 3.7 中你可以这样做:
from bokeh.palettes import Category10_10
color = Category10_10.__iter__()
p.line(x, y1, line_width=2, color=next(color))
这将循环遍历列表的每个元素,直到您每次使用 next()
。
python 中的每个序列类型都可以 return 一个迭代器对象。