如何用 plotly 绘制离散颜色 [英] How to plot discrete colours with plotly

查看:55
本文介绍了如何用 plotly 绘制离散颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我的代码:

fig2 = plotly.subplots.make_subplots(rows=3, cols=1, shared_xaxes=True)
fig2.append_trace(
        go.Scatter(x=df['Date_Time'], y=df["N2O_rSig"]), row=1, col=1)
                   
fig2.append_trace(                   
        go.Scatter(x=df['Date_Time'], y=df["Flow_rSig"]), row=2, col=1)
        
fig2.append_trace(   
        go.Scatter(x=df['Date_Time'], y=df["O2_rSig"]), row=3, col=1)
   
fig2.update_layout(title_text="Stacked Subplots")

fig2.write_html("test_plotly.html")

对于每条轨迹,我都希望有一个由 Valve_ai 控制的离散颜色,但我似乎找不到正确的方法.有没有一种无需重建的方法,将我的数据发送到 Plotly.graph_objects.

For each trace I want to have a discrete color governed by Valve_ai but I just can't seem to find the right way. Is there a way without having to rebuild that way my data is sent to Plotly.graph_objects.

我注意到 Plotly Express 能够根据变量颜色"拆分长数据.Cufflinks 还通过类别来管理这一点.然而,我为了管理跨多个子图的 Legends Plotly.go 似乎是唯一的选择.

I noticed Plotly Express has the ability to split long data based on the variable "color". Cufflinks also manages this with categories. However I in order to manage Legends across multiple subplots Plotly.go seems like the only option.

这是示例数据

,Unnamed: 0,Date_Time,N,Valve_ai,N2O_rSig,NO_rSig,O2_rSig,CO2_rSig,Flow_rSig
48,57,2020-07-15 00:00:57,58,Bio1 G1,6.33,16.69,20.61,1.0,1.02
49,58,2020-07-15 00:00:58,59,Bio1 G1,6.13,16.62,20.61,1.0,0.96
50,59,2020-07-15 00:00:59,60,Bio1 G1,6.15,16.56,20.6,1.0,0.98
51,60,2020-07-15 00:01:00,61,Bio1 G1,6.12,16.55,20.59,1.0,0.86
52,61,2020-07-15 00:01:01,62,Bio1 G1,6.44,16.68,20.6,1.0,1.07
53,62,2020-07-15 00:01:02,63,Bio1 G1,6.69,16.63,20.59,1.0,0.94
54,64,2020-07-15 00:01:04,65,Bio1 G2,7.28,16.69,20.57,1.0,0.98
55,65,2020-07-15 00:01:05,66,Bio1 G2,7.98,17.06,20.49,1.0,1.05
56,66,2020-07-15 00:01:06,67,Bio1 G2,8.82,17.37,20.4,1.0,0.98
57,67,2020-07-15 00:01:07,68,Bio1 G2,10.03,17.78,20.26,1.0,0.9
58,68,2020-07-15 00:01:08,69,Bio1 G2,13.4,19.36,19.94,1.0,1.02
59,69,2020-07-15 00:01:09,70,Bio1 G2,15.55,20.68,19.77,1.0,0.85

推荐答案

所以经过一番环顾后,我发现了与条形图的直接比较在情节网站上

So after a bit of looking around I found a direct comparison with bar graphs on the plotly website

以下代码允许我根据 Valve 拆分数据帧,然后遍历每个 DF 以创建单独的跟踪.

The following code allowed me to split up my Data Frame based on Valve and then iterate through each DF to create individual traces.

for valve, group in df.groupby("Valve_ai"):
        fig.add_trace(go.Scatter(x=group["Date_Time"], y=group["N2O_rSig"], name=valve)

这篇关于如何用 plotly 绘制离散颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆