[英]How to retain 1 label on yaxis for facet plot in plotly?
我從https://plotly.com/python/facet-plots/得到這個例子。 如何使 yaxis label 只有 1 個 label “lifeExp”,而不是所有 yaxes 的地塊都有 label?
import plotly.express as px
df = px.data.gapminder().query("continent == 'Africa'")
fig = px.line(df, x="year", y="lifeExp", facet_col="country", facet_col_wrap=7,
facet_row_spacing=0.04, # default is 0.07 when facet_col_wrap is used
facet_col_spacing=0.04, # default is 0.03
height=600, width=800,
title="Life Expectancy in Africa")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.update_yaxes(showticklabels=True)
fig.show()
您可以替換每個 plot 的 y 軸標題,然后為整個布局添加注釋,類似於 x 軸:
import plotly.express as px
df = px.data.gapminder().query("continent == 'Africa'")
fig = px.line(df, x="year", y="lifeExp", facet_col="country", facet_col_wrap=4,
facet_row_spacing=0.02,
facet_col_spacing=0.02,
height=1200, width=1000,
title="Life Expectancy in Africa")
fig.add_annotation(text=fig['layout']['yaxis']['title']['text'],
xref="paper", yref="paper",
x=-0.07, y=0.5, showarrow=False,textangle=-90)
fig.add_annotation(text=fig['layout']['xaxis']['title']['text'],
xref="paper", yref="paper",
x=0.5, y=-0.07, showarrow=False)
fig.update_yaxes(title="")
fig.update_xaxes(title="")
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.