簡體   English   中英

使用 Plotly 文件的 csv 文件的多個 Y 軸圖表

[英]Muliple Y-Axes charts from csv file using Plotly

我開發了一個軟件,可以從 csv 文件中讀取數據,並以不同類型的圖表顯示數據。

我知道可以在圖表中添加多個 Y 軸。

實際上,我的代碼正確顯示了每個帶有單獨軸的圖表。

如何改進我的代碼為每個圖表的每一行創建多個 Y 軸?

我的 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

我的代碼:

from plotly.offline import plot
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

class Plot_data:
    
    def line_plot_from_csv(self, file_name):
        
        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], mode='lines', name=df.columns[i]))
        
        fig.update_layout(title_text="Line plots", yaxis={'title':'Values [unit]'}, xaxis={'title':'DateTime'})        
        plot(fig) 
        
    
    def bar_plot_from_csv(self, file_name):
        
        df = pd.read_csv(file_name, delimiter=';')
        
        fig = make_subplots(rows=1, cols=df.shape[1]-1)
        
        for i in range(1, len(df.columns)):
            fig.add_trace(go.Bar(x=df.iloc[:,0], y=df.iloc[:,i], name=df.columns[i]),row=1, col=i)

        fig.update_layout(height=600, width=1000, title_text="Bar Subplots", yaxis={'title':'Values [unit]'}, xaxis={'title':'DateTime'})        
        plot(fig)
        
    def scatter_plot_from_csv(self, file_name):
        
        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], mode='markers', name=df.columns[i]))
        
        fig.update_layout(title_text="Markers plot", yaxis={'title':'Values [unit]'}, xaxis={'title':'DateTime'})
        plot(fig) 
        
    def subplots_from_csv(self, file_name):

        df = pd.read_csv(file_name, delimiter = ';')
        
        fig = make_subplots(rows=1, cols=df.shape[1]-1)
        
        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]),row=1, col=i)
        
        fig.update_layout(height=600, width=1000, title_text="Scatter line Subplots", yaxis={'title':'Values [unit]'}, xaxis={'title':'DateTime'})
        plot(fig) 

結果: https://drive.google.com/drive/folders/1VYkf5lCsixyCmhVmyClgXX9G8nss1q_w?usp=sharing

使用 'make_subplot' 指定矩陣、列寬和行寬,以及圖形的類型和 position。

import pandas as pd
import io
from plotly.offline import plot
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.read_csv(io.StringIO(data), delimiter = ';')

fig = go.Figure()

fig = make_subplots(
    rows=6, cols=4,
    column_widths=[0.25, 0.25, 0.25, 0.25],
    row_heights=[0.16,0.16,0.16,0.16,0.16,0.16],
    specs=[[{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}],
          [{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}],
          [{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}],
          [{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}],
          [{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}],
          [{"type": "scatter"},{"type": "bar"},{"type": "scatter"},{"type": "scatter"}]])

for i in range(1,len(df.columns)):
    fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,i], mode='lines', name=df.columns[i]), row=i, col=1)
    fig.add_trace(go.Bar(x=df.iloc[:,0], y=df.iloc[:,i], name=df.columns[i]),row=i, col=2)
    fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,i], mode='markers', name=df.columns[i]),row=i, col=3)
    fig.add_trace(go.Scatter(x=df.iloc[:,0], y=df.iloc[:,i], name=df.columns[i]),row=i, col=4)
    
plot(fig)

在此處輸入圖像描述

多軸 Y 軸:

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,1],
    name="S1 data"
))


fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,2],
    name="S2 data",
    yaxis="y2"
))

fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,3],
    name="S3 data",
    yaxis="y3"
))

fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,4],
    name="S4 data",
    yaxis="y4"
))

fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,5],
    name="S5 data",
    yaxis="y5"
))

fig.add_trace(go.Scatter(
    x=df.iloc[:,0],
    y=df.iloc[:,6],
    name="S6 data",
    yaxis="y6"
))




fig.update_layout(
    xaxis=dict(
        domain=[0.3, 0.7]
    ),
    yaxis=dict(
        title="yaxis title",
        titlefont=dict(
            color="#1f77b4"
        ),
        tickfont=dict(
            color="#1f77b4"
        ),
        side="left",
        position=0.10        
    ),
    yaxis2=dict(
        title="yaxis2 title",
        titlefont=dict(
            color="#ff7f0e"
        ),
        tickfont=dict(
            color="#ff7f0e"
        ),
        anchor="free",
        overlaying="y",
        side="left",
        position=0.20
    ),
    yaxis3=dict(
        title="yaxis3 title",
        titlefont=dict(
            color="#d62728"
        ),
        tickfont=dict(
            color="#d62728"
        ),
        anchor="x",
        overlaying="y",
        side="left",
        position=0.30
    ),
    yaxis4=dict(
        title="yaxis4 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="y",
        side="right",
        position=0.70
    ),
    yaxis5=dict(
        title="yaxis5 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="y",
        side="right",
        position=0.80
    ),
    yaxis6=dict(
        title="yaxis6 title",
        titlefont=dict(
            color="#9467bd"
        ),
        tickfont=dict(
            color="#9467bd"
        ),
        anchor="free",
        overlaying="y",
        side="right",
        position=0.90
    )
)



# Update layout properties
fig.update_layout(
    title_text="multiple y-axes example",
    width=800,
)

fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM