[英]Multiple boxplots in subplots with plotly
我正在嘗試創建一個帶有來自 plotly 的子圖的多重箱線圖應用程序,它讓我知道由不同標准分隔的變量的演變。
我的代碼如下所示:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
trace0 = go.Box(
y=df[(df.tipo == 'n')]['p'],
x=df[(df.tipo == 'n')]['m'],
name='N'
)
trace1 = go.Box(
y=df[(df.tipo == 's')]['p'],
x=df[(df.tipo == 's')]['m'],
name='S'
)
fig = make_subplots(rows=1, cols=2)
fig.append_trace(trace0, row = 1, col = 1)
fig.append_trace(trace1, row = 1, col = 2)
fig.show()
這個輸出看起來像:
我想要得到的是看起來像這樣的東西,但是對於每個子圖:
我確實有兩個虛擬變量,可以讓我在子圖中將其分開。
謝謝
import pandas as pd
import numpy as np
import plotly.express as px
from plotly.subplots import make_subplots
df = pd.DataFrame({**{"month":np.tile(pd.date_range("1-jul-2020", freq="M", periods=12),20)[0:200],
"tipo":np.random.choice(["N","S"],200)},
**{f"var{v}":np.random.uniform(2,5,200) for v in range(8)}})
fig = make_subplots(
rows=3, cols=3, subplot_titles=[c for c in df.columns if "var" in c]
)
for v in range(8):
for t in px.box(df, x="month", y=f"var{v}", color="tipo").data:
fig.add_trace(t, row=(v//3)+1, col=(v%3)+1)
# modifications needed to fix up display of sub-plots
fig.update_layout(
boxmode="group", margin={"l": 0, "r": 0, "t": 20, "b": 0}
).update_traces(showlegend=False, selector=lambda t: "var0" not in t.hovertemplate)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.