如何在Plotly中使多面图具有自己的单独YAxes刻度标签?

问题描述 投票:0回答:1

[当我使用Plotly express绘制具有不同范围的不同参数时-在下面的示例中,BloodPressureHigh,Height(cm),Weight(kg)和BloodPressureLow-使用facet_col参数时,我无法获得结果图为每个多面图显示唯一的YTicks。有没有一种简单的方法可以使fig对象在生成的多面图中显示每组YTicks?否则,正如您在结果图像中看到的那样,不清楚每个箱形图是否在其自己的唯一YAxis上。

import plotly.express as px
import pandas as pd

temp = [
    {"Clinic": "A", "Subject": "Bill", "Height(cm)": 182, "Weight(kg)": 101, "BloodPressureHigh": 128, "BloodPressureLow": 90},
    {"Clinic": "A", "Subject": "Susie", "Height(cm)": 142, "Weight(kg)": 67, "BloodPressureHigh": 120, "BloodPressureLow": 70},
    {"Clinic": "B", "Subject": "John", "Height(cm)": 202, "Weight(kg)": 89, "BloodPressureHigh": 118, "BloodPressureLow": 85},
    {"Clinic": "B", "Subject": "Stacy", "Height(cm)": 156, "Weight(kg)": 78, "BloodPressureHigh": 114, "BloodPressureLow": 76},
    {"Clinic": "B", "Subject": "Lisa", "Height(cm)": 164, "Weight(kg)": 59, "BloodPressureHigh": 112, "BloodPressureLow": 74} 
]
df = pd.DataFrame(temp)

# Melt the dataframe so I can use plotly express to plot distributions of all variables
df_melted = df.melt(id_vars=["Clinic", "Subject"])
# Plot distributions, with different parameters in different columns
fig = px.box(df_melted, x="Clinic", y="value", 
       facet_col="variable", boxmode="overlay"
)
# Update the YAxes so that the faceted column plots no longer share common YLimits
fig.update_yaxes(matches=None)
# Last step needed: Add tick labels to each yaxis so that the difference in YLimits is clear?

enter image description here

python pandas plotly plotly-express
1个回答
0
投票

这对您有帮助吗?

fig = px.box(df_melted, x="Clinic", y="value", 
             facet_col="variable", boxmode="overlay")

fig.update_yaxes(matches=None)
for i in range(len(fig["data"])):
    yaxis_name = 'yaxis' if i == 0 else f'yaxis{i + 1}'
    fig.layout[yaxis_name].showticklabels = True

fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.