gpt4 book ai didi

python - 使用 Bokeh 绘图时,如何自动循环显示调色板?

转载 作者:IT老高 更新时间:2023-10-28 20:52:45 26 4
gpt4 key购买 nike

我想使用循环加载和/或修改数据并使用 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)

生成此图

bokeh plot

如何使颜色循环而不编码颜色列表和模数运算以在颜色数量用完时重复?

在 GitHub 上有一些与此相关的讨论,问题 3512201 ,但目前尚不清楚如何使这项工作。我在搜索 documentation 时得到的四次点击for cycle color 实际上并没有在页面的任何位置包含单词 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)

enter image description here

关于python - 使用 Bokeh 绘图时,如何自动循环显示调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39839409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com