gpt4 book ai didi

Python Bokeh : Set line color based on column in columndatasource

转载 作者:太空狗 更新时间:2023-10-30 00:16:56 25 4
gpt4 key购买 nike

我正在尝试制作一个包含多条线的图表,但我使用的数据通常采用长格式,如下所示:

x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
df = pandas.DataFrame(data=[x,y0,y1,y2]).T
df.columns = ['x','y0','y1','y2']

df2 = pd.concat([df.iloc[0:,1],df.iloc[0:,2],df.iloc[0:,3]], axis=0, keys = ['a','b','c']).reset_index()
df2.columns = ['grp','x','y']

df2

+----+-----+---+----------+
| | grp | x | y |
+----+-----+---+----------+
| 0 | a | 0 | 0.01 |
| 1 | a | 1 | 0.25 |
| 2 | a | 2 | 1.00 |
| 3 | a | 3 | 2.25 |
| 4 | a | 4 | 4.00 |
| 5 | a | 5 | 6.25 |
| 6 | a | 6 | 9.00 |
| 7 | b | 0 | 1.26 |
| 8 | b | 1 | 3.16 |
| 9 | b | 2 | 10.00 |
| 10 | b | 3 | 31.62 |
| 11 | b | 4 | 100.00 |
| 12 | b | 5 | 316.23 |
| 13 | b | 6 | 1,000.00 |
+----+-----+---+----------+

cd_df2 = ColumnDataSource(df2)

也就是说,我会有“组”,其中每个组的 x,y 对在多行中列出。

以下生成所有 3 行,但它们都显示为灰色。设置 color = 'grp' 不会为列数据源中 grp 字段中的每个值指定颜色

f = figure(tools="save",y_axis_type="log", y_range=[0.001, 10**11], 
title="log axis example",x_axis_label='sections', y_axis_label=
'particles')

f.line('x','y', line_color = 'grp', source = cd_df2)

我如何在 bokeh.plotting 或 bokeh.models api 中实现这一点(想要避免高级图表以更好地理解库)?我愿意接受其他建议,避免为每一行显式调用一次 f.line() 并单独设置颜色(我可能有 10 多行,这会变得乏味)。

最佳答案

您可以重复使用 this example 中描述的原理.它基于“补丁”,但对于“线路”是相同的(参见 http://docs.bokeh.org/en/latest/docs/reference/plotting.html ):

p = figure()    
p.patches([x2 for a in areas], list(areas.values()), color=colors, alpha=0.8, line_color=None)

关于Python Bokeh : Set line color based on column in columndatasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645238/

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