[英]Single axis caption in plotly express facet plot
我正在學習使用 pyplot.express 並努力解決以下設計問題:在多面圖中,每個子圖都重復軸標題(在示例中為“花瓣寬度(厘米)”)。 有沒有辦法使用 pyplot.express 為多面圖上的所有子圖獲得單軸 label?
謝謝,邁克爾
最小的例子:
from sklearn.datasets import load_iris
import plotly.express as px
import pandas as pd
import numpy as np
# import iris-data
iris = load_iris()
df= pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns= iris['feature_names'] + ['target'])
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
# plot using pyplot.express
fig = px.bar(df, x="sepal length (cm)", y="petal width (cm)", color = 'petal length (cm)', facet_row="species")
fig.show()
感謝@vestland 幫了大忙!
根據您的回答,我想出了一種更靈活的設計(多個 facet_rows)的方法:
首先,我需要刪除所有子圖軸:
for axis in fig.layout:
if type(fig.layout[axis]) == go.layout.YAxis:
fig.layout[axis].title.text = ''
下一步是添加注釋而不是軸,因為布局中的 yaxis 屬性總是會修改其中一個軸的縮放比例並弄亂 plot。 搜索注釋,我找到了如何添加自定義軸的鏈接。 獨立於子圖的 position 和 label 需要xref='paper'和yref='paper' 。
fig.update_layout(
# keep the original annotations and add a list of new annotations:
annotations = list(fig.layout.annotations) +
[go.layout.Annotation(
x=-0.07,
y=0.5,
font=dict(
size=14
),
showarrow=False,
text="Custom y-axis title",
textangle=-90,
xref="paper",
yref="paper"
)
]
)
這是執行此操作的 Plotly 本機方法
fig.update_yaxes(title='')
fig.update_layout(yaxis2=dict(title="Custom Title"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.