gpt4 book ai didi

Python Bokeh : Multiple color segments on same line

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:48 24 4
gpt4 key购买 nike

我正在使用 python 3.5 和 bokeh 0.12.1,我正在尝试在不同的线段上绘制一条具有多种颜色的简单线条。基本上我希望该行根据列值具有不同的颜色。这是我的代码的简化版本:

import numpy as np
from numpy import vectorize
import pandas as pd
from bokeh.plotting import figure, show, output_file


def f(x):
return 2 * x


def color(x):
if x < 20:
return 0
if 20 <= x < 60:
return 1
if 60 <= x < 80:
return 0
else:
return 1


v_color = vectorize(color)
x = np.arange(0, 100, 1)
data = {'x': x, 'y': f(x), 'colors': v_color(x)}
df = pd.DataFrame(data=data)
# print(df)

p = figure(title="Line example")

p.line(df['x'], df['y'], legend="y=f(x)",
# line_color="tomato",
line_color="olivedrab",
line_width=2)

p.legend.location = "top_left"

output_file("basic_line_test.html", title="line plot example")

show(p) # open a browser

基本上,该行应该有一种颜色,假设列“colors”为 0 时为“olivedrab”,值为 1 时为“tomato”。我该怎么做?

最佳答案

从 Bokeh 0.12.1 开始,目前不支持此功能。线条一次只能有一种颜色。您的下一个最佳选择是尝试使用 multi_linesegments 字形函数,但这样做会更冗长(您将必须计算并提供起点/终点每个单独的分割市场)。

这可能会在未来的某个版本中作为一项功能添加,我鼓励您在项目 GitHub issue tracker 上提交功能请求.

关于Python Bokeh : Multiple color segments on same line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169197/

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