繁体   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