繁体   English   中英

使用 Plotly 的子图条形图

[英]Subplot bar graphs using Plotly

我的 csv 文件:

DateTime;S1;S2;S3;S4;S5;S6
2020-07-17 09:57:27.119916;725.9926027110598;730.5730869210306;946.2937510737263;542.341137182406;758.5531610786929;512.2027881299339
2020-07-17 09:57:28.119916;761.1846087077208;984.1009029835216;974.8724733720549;576.8019892357476;751.6553704523698;855.5439493088621
2020-07-17 09:57:29.119916;618.7837289058051;823.9970681226491;594.2841714340789;873.3093170922189;770.0875733375253;681.1715820388949
2020-07-17 09:57:30.119916;515.9456035777555;533.017970929369;639.3409213385498;542.4405737836958;514.4985515824058;650.5229638670448
2020-07-17 09:57:31.119916;589.1350057317254;605.703259361724;602.3181712775759;860.9749699475683;801.7960812507487;562.400896160191
2020-07-17 09:57:32.119916;626.2528314431347;615.7078057434281;643.2023497200336;709.6997180536518;741.365852401098;712.4384053449293
2020-07-17 09:57:33.119916;553.9768845577024;961.7714859567449;519.8207498752649;551.8006708566627;511.7426656331682;849.3428394570542
2020-07-17 09:57:34.119916;994.8208541190293;700.59423301376;569.1853469890981;997.5842090634065;621.2070112896865;848.5079857917269
2020-07-17 09:57:35.119916;502.2301607876932;760.8787524302393;671.2907579865052;669.0718770518221;901.3788876259023;926.077760311429
2020-07-17 09:57:36.119916;578.3978109170034;811.407262562966;822.6244615030105;570.0016494663124;935.0853062150045;689.8800124555897

我正在尝试使用 X 坐标上的DateTime和 Y 坐标上的每个信号值为每个S1,S2,etc...创建子图。 这是我的代码:

df = pd.read_csv(file_name, delimiter = ';')

for i in range(1, len(df.columns)):
    col_name = 'S'+ str(i)
    fig = px.bar(df, x=df['DateTime'], y=df[col_name], facet_col=col_name)

plot(fig) 

但这是我的 output: https://drive.google.com/file/d/1LwQpq7vdYCeLrz6hdDG6cmaBmzM0SvyP/view?usp=sharing

我该如何解决?

您可以使用add_traceplotly go (一种解决方法)

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=df.shape[1]-1)
fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")

for i in range(1, len(df.columns)):
    col_name = 'S'+ str(i)
    fig.add_trace(
        go.Bar(x=df['DateTime'], y=df[col_name], name=col_name),
    row=1, col=i
)

fig.show()

在此处输入图像描述

请注意,在您的 for 循环中,您将多次覆盖 fig 。 您可以改为使用子图

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=6)

df = pd.read_csv(file_name, delimiter=';')

for i in range(1, 7):
    col_name = 'S' + str(i)
    fig.append_trace(go.Bar(x=df['DateTime'], y=df[col_name]), row=1, col=i)

plot(fig)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM