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:
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. I want to make subplots of each df.
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()
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.