簡體   English   中英

如何在 plotly 中創建帶有 6 行子圖的 plot?

[英]How do I create a plot with 6 line subplots in plotly?

我有 6 個不同的數據框,我想制作 1 個圖形,顯示 6 個圖形,x 軸為年份,y 軸為每 100k 的事件案例。 我一直在使用以下代碼來創建我想要的單個圖形,但現在我被困在如何將它們全部放入一個圖形中。

px.line(gyeEUR, x="year", y="incident cases per 100k", color="country", title="Incident Cases per 100k in Europe")

我只是想要這個 plot 6 次,使用 6 個不同的數據幀(即 gyeEUR,gyeEMR....)。 go 關於這個的最佳方法是什么?

您可能會考慮讀取所有 6 個數據幀,向它們添加一列df_i["fn"]= "filename" ,最后將它們連接到更大的 dataframe df並使用

px.line(df,
        x="year",
        y="incident cases per 100k",
        color="country",
        title="Incident Cases per 100k in Europe",
        facet_row="fn")

更新:完整示例

生成數據

這里我們簡單讀取gapminder內置plotly.express的數據,並拆分為大陸的幾個文件。 鑒於大陸在文件名中,我們將該列放在每個組中

import os
import plotly.express as px
os.makedirs("data", exist_ok=True)

df = px.data.gapminder()

df.groupby("continent")\
  .apply(lambda x: 
         x.drop("continent", axis=1)\
          .to_csv("data/{}.csv".format(x.name),
                  index=False))

讀取數據並連接

# list all csv in data/
files = os.listdir("data/")
files = list(filter(lambda f: f.endswith('.csv'), files))

# read, add continent and concat

out = []
for file in files:
    df = pd.read_csv(f"data/{file}")
    df["continent"] = file[:-4]
    out.append(df)
df = pd.concat(out)

Plot

這看起來不太好,但這只是一個例子

px.line(df,
        x="year",
        y="pop",
        color="country",
        facet_row="continent")

暫無
暫無

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

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