簡體   English   中英

plotly express facet plot 中的單軸標題

[英]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()

虹膜刻面圖

對於這種特殊情況,在您的示例片段之后,只需運行

fig['layout']['yaxis']['title']['text']=''
fig['layout']['yaxis3']['title']['text']=''
fig.show()

Plot:

在此處輸入圖像描述

感謝@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"
        )
    ]
)

pyplot

這是執行此操作的 Plotly 本機方法

    fig.update_yaxes(title='')
    fig.update_layout(yaxis2=dict(title="Custom Title"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM