将数据添加到带有痕迹的迹线中,以保留虚线中每个子图的数据分割

问题描述 投票:0回答:1

我有虚拟数据框:

import pandas as pd
df=pd.DataFrame({'A':[1,2,3,20,30,40],'B':['Tita','Tita','Tita','Burru','Burru','Burru'],'Z':[1,2,3,1,2,3]})

我想为B列中的每个值(Tita和Burru)提供一个子图。

此代码产生预期的输出:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
lista_syst=df.B.unique()

fig.add_trace(
    go.Scatter(x=df.loc[df['B'] == 'Tita', 'A'], y=df.loc[df['B'] == 'Tita', 'Z']),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=df.loc[df['B'] == 'Burru', 'A'], y=df.loc[df['B'] == 'Burru', 'Z']),
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Subplots")
fig.show()

enter image description here

但是我想自动对B列中的[[n个可能的值:

from plotly.subplots import make_subplots import plotly.graph_objects as go df=pd.DataFrame({'A':[1,2,3,20,30,40],'B':['Tita','Tita','Tita','Burru','Burru','Burru'],'Z':[1,2,3,1,2,3]}) fig = make_subplots(rows=1, cols=2) lista_syst=df.B.unique() for sist in lista_syst: print(sist) print(df.loc[df['B'] == sist, 'A']) for i in range(1,3): fig.add_trace( go.Scatter(y=df.loc[df['B'] == sist, 'A'],x=df.loc[df['B'] == sist, 'Z']), row=1,col=i )
这最后一个代码,返回两个具有所有值的重复图形(对于B中的两个值,相同图形两次),怎么办?

enter image description here

可以做我想做的事吗?

python plotly-dash
1个回答
0
投票
您是否正在寻找类似的东西?

from plotly.subplots import make_subplots import plotly.graph_objects as go df=pd.DataFrame({'A':[1,2,3,20,30,40],'B':['Tita','Tita','Tita','Burru','Burru','Burru'],'Z':[1,2,3,1,2,3]}) fig = make_subplots(rows=1, cols=2) lista_syst=df.B.unique() for sist in lista_syst: print(sist) print(df.loc[df['B'] == sist, 'A']) fig.add_trace( go.Scatter( x=df.loc[df['B'] == sist, 'Z'], y=df.loc[df['B'] == sist, 'A'])) fig.update_layout(height=600, width=800, title_text="Subplots") fig.show()

这为您提供:enter image description here

评论后编辑:如果您希望各个图彼此相邻,则可以执行以下操作:

from plotly.subplots import make_subplots import plotly.graph_objects as go df=pd.DataFrame({'A':[1,2,3,20,30,40],'B':['Tita','Tita','Tita','Burru','Burru','Burru'],'Z':[1,2,3,1,2,3]}) fig = make_subplots(rows=1, cols=2) lista_syst=df.B.unique() i=0 for sist in lista_syst: i=i+1 fig.add_trace( go.Scatter(x=df.loc[df['B'] == sist, 'A'],y=df.loc[df['B'] == sist, 'Z']), row=1,col=i ) fig.update_layout(height=600, width=800, title_text="Subplots") fig.show()
这为您提供:enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.