简体   繁体   English

Plotly for 循环中箱线图的子图 Python

[英]Plotly subplots of boxplot in for-loop Python

How can I make subplots by using a for loop when the traces for each individual subplot are already created using a for loop, what I got so far:当已经使用 for 循环创建了每个单独的子图的跟踪时,我如何通过使用 for 循环制作子图,到目前为止我得到的是:

import plotly.express as px
import plotly.io as pio 
import kaleido 
pio.renderers.default='browser' #change to browser or svg
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df1 = pd.DataFrame({'in1' : [100, 150, 110, 180, 125], 
                   'in2' : [200, 210, 125, 125, 293],
                   'in3' : [50, 35, 200, 100, 180]
                   })

a = ['c', 'd', 'e', 'f','g']
df1t = df1.T
df1t.columns= a

fig = make_subplots()

for col in df1t:
    fig.add_trace(go.Box(y=df1t[col].values, name=str(df1t[col].name)))
    
fig.show()

But I have multiple dataframes named df2, df3, df4, df5, df6, df7, df8 and df9 which are plotted in the same way as above with column name a.但是我有多个名为 df2、df3、df4、df5、df6、df7、df8 和 df9 的数据帧,它们的绘制方式与上面相同,列名为 a。 I want to make subplots of each df.我想为每个 df 制作子图。

  • have synthesised 9 additional data frames per your specification根据您的规格合成了 9 个额外的数据帧
  • construct a dict of all defined variables df1 to df9.构造所有已定义变量 df1 到df9的字典。 This is to enable simple looping这是为了启用简单循环
  • now just a simple case of creating all the traces across all the data frames after creating the figure with appropriate number of rows现在只是一个简单的例子,在创建具有适当行数的图形后,跨所有数据帧创建所有轨迹
import plotly.express as px
import plotly.io as pio
import kaleido
import pandas as pd
import numpy as np

pio.renderers.default = "browser"  # change to browser or svg
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df1 = pd.DataFrame(
    {
        "in1": [100, 150, 110, 180, 125],
        "in2": [200, 210, 125, 125, 293],
        "in3": [50, 35, 200, 100, 180],
    }
)

# fmt: off
df2 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df3 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df4 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df5 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df6 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df7 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df8 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
df9 = pd.DataFrame((df1.values * np.random.uniform(0.8, 1.2, size=df1.shape)).astype(int),columns=df1.columns)
# fmt: on

# create dictionary of dataframes, by inspecting defined variables
dfs = {
    varname: eval(varname)
    for varname in dir()
    if varname[0:2] == "df" and len(varname) == 3 and varname[2] in "123456789"
}

a = ["c", "d", "e", "f", "g"]

# create sub-plots based on number of dataframes
fig = make_subplots(rows=len(dfs.keys()))

# add traces to appropriate subplot for dataframe
for r, df in enumerate(dfs.values()):
    df1t = df.T
    df1t.columns = a

    for col in df1t:
        fig.add_trace(
            go.Box(y=df1t[col].values, name=str(df1t[col].name)), row=r + 1, col=1
        )

fig.show()

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

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