如何在Plotly中使刻面图具有自己的Yax刻度标签?

2024-05-19 01:38:18 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用Plotly express以不同的范围绘制不同的参数时——在下面的示例中,血压高、身高(cm)、体重(kg)和血压低——使用facet_col参数时,我无法获得结果图来显示每个刻面图的唯一Ytick。有没有一种简单的方法让fig对象在生成的刻面图中显示每组Ytick?否则,正如您在生成的图像中所看到的,不清楚每个方框图是否位于其唯一的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


Tags: thedf参数sofigcmfacetsubject
1条回答
网友
1楼 · 发布于 2024-05-19 01:38:18

这对你有帮助吗

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

fig.update_yaxes(matches=None)
fig.for_each_yaxis(lambda yaxis: yaxis.update(showticklabels=True))
    
fig.show()

enter image description here

相关问题 更多 >

    热门问题