简体   繁体   English

Plotly 图形具有相同的 X 轴和多个 Y 轴,数据数量未定义

[英]Plotly graph with same X and multiple Y axes with undefined number of data

I've developed a software that reads data from csv file and show data in a chart with multiple Y axes.我开发了一个软件,可以从 csv 文件中读取数据,并在具有多个 Y 轴的图表中显示数据。 For each signal in my csv file I have a line in the chart and for each line in the chart there is an axes.对于我的 csv 文件中的每个信号,我在图表中有一条线,并且对于图表中的每条线,都有一个轴。

If the number of signals contained in csv file is unknown, so there are more than six for example (S1...S7,S8,S9,etc....), how can I auto generate Y axis for each signal??如果 csv 文件中包含的信号数量未知,例如有六个以上(S1...S7、S8、S9 等...),如何为每个信号自动生成 Y 轴?

Now my program is build like I know the number of signals in the csv file.现在我的程序就像我知道 csv 文件中的信号数量一样构建。 I've already tried to create a for that modify the keyword yaxis but is not possible.我已经尝试for修改关键字yaxis创建一个,但这是不可能的。

My csv file:我的 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

My code:我的代码:

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

        for i in range(1, len(df.columns)):
            fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,i], name=df.columns[i], yaxis='y'+str(i)))
        
        fig.update_layout(
            xaxis=dict(
                domain=[0.3, 0.7]
            ),
            yaxis=dict(
                title="S1",
                titlefont=dict(
                    color="#1f77b4"
                ),
                tickfont=dict(
                    color="#1f77b4"
                ),
                side="left",
                position=0.10        
            ),
            yaxis2=dict(
                title="S2",
                titlefont=dict(
                    color="#ff7f0e"
                ),
                tickfont=dict(
                    color="#ff7f0e"
                ),
                anchor="free",
                overlaying="y",
                side="left",
                position=0.20
            ),
            yaxis3=dict(
                title="S3",
                titlefont=dict(
                    color="#d62728"
                ),
                tickfont=dict(
                    color="#d62728"
                ),
                anchor="x",
                overlaying="y",
                side="left",
                position=0.30
            ),
            yaxis4=dict(
                title="S4",
                titlefont=dict(
                    color="#6427bd"
                ),
                tickfont=dict(
                    color="#6427bd"
                ),
                anchor="free",
                overlaying="y",
                side="right",
                position=0.70
            ),
            yaxis5=dict(
                title="S5",
                titlefont=dict(
                    color="#3758bd"
                ),
                tickfont=dict(
                    color="#3758bd"
                ),
                anchor="free",
                overlaying="y",
                side="right",
                position=0.80
            ),
            yaxis6=dict(
                title="S6",
                titlefont=dict(
                    color="#2690bd"
                ),
                tickfont=dict(
                    color="#2690bd"
                ),
                anchor="free",
                overlaying="y",
                side="right",
                position=0.90
            )
        )
        
        fig.update_layout(
            title_text="Stacked y-axes",
            yaxis={'title':'Values [unit]'}, 
            xaxis={'title':'DateTime'},
            height=600, 
            width=1000
        )
        
        plot(fig)

This is my result now: https://drive.google.com/file/d/1QPLG0gT2SvWd4_0-piGQsdRy_eTACFn1/view?usp=sharing这是我现在的结果: https://drive.google.com/file/d/1QPLG0gT2SvWd4_0-piGQsdRy_eTACFn1/view?usp=sharing

You can build all the yaxis* arguments inside your for loop.您可以在 for 循环中构建所有 yaxis yaxis* arguments。 In the example below, I create a dict named axes , add each yaxis* subdict and later I call fig.update_layouts(**axes) .在下面的示例中,我创建了一个名为axes的字典,添加每个 yaxis yaxis*子字典,然后我调用fig.update_layouts(**axes)

df = pd.read_csv('filename.csv', delimiter = ';')
        
fig = go.Figure()

colors=["#1f77b4","#ff7f0e","#d62728","#6427bd","#3758bd","#2690bd"]

axes = {
    'xaxis': dict(domain=[0.3, 0.7])
}

for i in range(1, len(df.columns)):
    fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,i], name=df.columns[i], yaxis='y'+str(i)))
    s = 'yaxis'
    if i > 1:
        s = s + str(i)
    axes[s] = dict(
        title=f"S{i}",
        titlefont=dict(color=colors[i-1]),
        tickfont=dict(color=colors[i-1])
    )
    if i > 1:
        axes[s]['anchor'] = 'free'
        axes[s]['overlaying'] = 'y'
    if i > len(df.columns) / 2:
        axes[s]['side'] = 'right'
        axes[s]['position'] = 0.3 + 0.1*i
    else:
        axes[s]['side'] = 'left'
        axes[s]['position'] = 0.1*i

fig.update_layout(**axes)
fig.update_layout(
    title_text="Stacked y-axes",
    yaxis={'title':'Values [unit]'}, 
    xaxis={'title':'DateTime'},
    height=600, 
    width=1000
)

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

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