【问题标题】:How can I make faceted plots in Plotly have their own individual YAxes tick labels?如何在 Plotly 中制作多面图有自己的 YAxes 刻度标签?
【发布时间】:2020-07-14 18:07:52
【问题描述】:

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

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?

【问题讨论】:

  • 您介意添加预期输出的图吗?
  • 谢谢@rpanai,下面你的答案中的输出正是我想要的。
  • 我觉得用shared_y会更好看。

标签: python pandas plotly plotly-express


【解决方案1】:

这对你有帮助吗?

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()

【讨论】:

  • 也可以一键设置fig.update_yaxes(matches=None, showticklabels=True).
  • @JanKislinger 不错,
猜你喜欢
  • 1970-01-01
  • 2020-04-24
  • 2021-10-25
  • 1970-01-01
  • 2014-06-30
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多