简体   繁体   中英

python plotly express mutiple layer graph (boxchart + scatter)

I want to create a multi layer graph with the same data frame from pandas. One should be a boxplot and the other a scatter to see where the company is located.

Is there a way to combine both plots?

boxplot在此处输入图像描述

scatterplot在此处输入图像描述

import pandas as pd 
import plotly.express as px

df = pd.read_csv("company_index.csv", sep=";", decimal=",")
print(df)

df_u9 = df.loc[df["company"].isin(["U9"])]

fig_1 = px.box(
        df,
        x="period",
        y="index"
    )
fig_2 = px.scatter(
        df_u9,
        x="period",
        y="index"
    )

fig_1.show()
fig_2.show()

company_index.csv period;index;company 1;202,4;U1 1;226,69;U10 1;235,18;U9 1;236,49;U4 1;238,13;U2 1;244,05;U6 1;252,08;U3 1;256,68;U8 1;294,99;U5 1;299,391;U7 2;243,78;U1 2;264,26;U10 2;270,6;U2 2;272,89;U9 2;285,26;U5 2;289,29;U4 2;291,15;U6 2;291,19;U3 2;305,92;U7 2;314,65;U8 3;271,82;U1 3;278,65;U2 3;296,16;U10 3;297,21;U4 3;305,93;U6 3;308,96;U5 3;323,74;U9 3;335,93;U3 3;354,13;U8 3;381,2;U7 4;281,26;U5 4;308,5;U2 4;311,61;U1 4;334,03;U4 4;335,72;U9 4;344,32;U8 4;345,27;U6 4;355,44;U3 4;373,54;U7 4;381,68;U10 5;288,6;U1 5;305,66;U5 5;323,2;U2 5;358,46;U8 5;365,57;U3 5;366,96;U10 5;368,38;U7 5;371,23;U6 5;373,63;U4 5;422,93;U9 6;285,32;U5 6;291,65;U1 6;308,68;U2 6;372,04;U8 6;376,64;U3 6;403,55;U6 6;407,38;U4 6;420,65;U10 6;423,68;U9 6;453,09;U7

Found this solution. Works rather well. Im still struggling to understand the ".data[0]" but i believe its referring to the first fig in use. Maybe if you have multiple graphs.

在此处输入图像描述

import pandas as pd 
import plotly.express as px

df = pd.read_csv("company_index.csv", sep=";", decimal=",")
print(df)

df_u9 = df.loc[df["company"].isin(["U9"])].copy()
df_u9["size"] = 1

fig = px.box(
        df,
        x="period",
        y="index"
    )
fig.add_trace(px.scatter(
        df_u9,
        x="period",
        y="index",
        size="size",
        size_max=15,
        color_discrete_sequence=(203,153,201)

    ).data[0])

fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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