X轴标签定位

问题描述 投票:0回答:3

我制作了一个多面图直方图,如下所示:

fig = px.histogram(df, 
                   x = "x column", 
                   color = "colour column",  
                   facet_col = 'facet column')
fig.update_xaxes(title='')
fig.update_layout(xaxis2=dict(title='lonnnnnnnnnnng x axis title'))
for anno in fig['layout']['annotations']:
    anno['text']=''
fig.update_layout({'title' : 'title'})

我得到了下图。有人可以解释如何将

xaxis2
标签 (
'lonnnnnnnnnnng x axis title'
) 居中,如下图所示吗?

谢谢!

python plotly label position x-axis
3个回答
0
投票

您将

xaxis
的标题更改为空,然后使用了标题
xaxis2
,您可以通过将标题设置为'lonnnnnnnnnnnng x axis title'来快捷操作。会是这样的:

fig.update_xaxes(title='lonnnnnnnnnnng x axis title')

无需第二行。


0
投票

与你的方法基本相同,但是为了居中,分面图的数量就是分面中变量的数量,因此使用了循环过程。通过将图形数量截断一半来将中心位置设置为第二个。如果你想将其设置得更靠近中心,唯一的方法就是使用注释功能。我的代码是使用此reference中的示例创建的。除了您的方法之外,禁用 x 轴标题的另一种方法是清空 x 轴变量的标签。这项技术的灵感来自于这个答案

import math
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color='sex', facet_col="day", labels={'total_bill':''})

for i,x in enumerate(df['day'].unique(),start=1):
    if i == math.floor(len(df['day'].unique()) / 2):
        facet = 'xaxis{}'.format(i)
        fig.layout[facet].title = title='lonnnnnnnnnnng x axis title'
    
fig.show()


0
投票

我相信这会起作用

fig.update_layout(title_x=<position from 0 (left side) to 1 (right side),
                  title_text="Title")

所以把它放在中间就可以了

fig.update_layout(title_x=0.5,
                  title_text="Title")
© www.soinside.com 2019 - 2024. All rights reserved.