在 Plotly 中自定义 Tornado 图表的 x 轴

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

如何设置两个镜像轴的单一起点?我用“make_subplot”函数构建了一个图表。但我在中心有一个空的空间。我使用来自 Tableau 数据收集的“Sample Superstore”数据集。

我的代码:

df_oct = df.loc[(df['order_date'] > '2017-09-30') & (df['order_date'] < '2017-11-01') & (df['region'] == 'Central')]
df_sep = df.loc[(df['order_date'] > '2017-08-31') & (df['order_date'] < '2017-10-01') & (df['region'] == 'Central')]

states = pd.DataFrame(
            df.loc[(df['order_date'] > '2017-08-31') & (df['order_date'] < '2017-11-01') & (df['region'] == 'Central'), 'state'].unique()
            ,columns=['state'])

df_oct = states.merge(df_oct.groupby(by='state', as_index=False)['sales'].sum(), how='left', on='state')
df_sep = states.merge(df_sep.groupby(by='state', as_index=False)['sales'].sum(), how='left', on='state')

df_oct['month'] = 'October'
df_sep['month'] = 'September'
tornado_df = pd.concat([df_oct, df_sep])
tornado_df.fillna(0, inplace=True)

fig = make_subplots(
        rows=1
        ,cols=2
        ,vertical_spacing=0
)

fig_add = fig.add_trace(
            go.Histogram(
                x=tornado_df.loc[tornado_df['month'] == 'September', 'sales']
                ,y=tornado_df.loc[tornado_df['month'] == 'September', 'state']
                ,histfunc='sum'
                ,orientation='h'
                ,marker_color='#D95002'
                ,name='September'
                ,opacity=0.6)
            ,row=1
            ,col=1)

fig_add = fig.add_trace(
            go.Histogram(
                x=tornado_df.loc[tornado_df['month'] == 'October', 'sales']
                ,y=tornado_df.loc[tornado_df['month'] == 'October', 'state']
                ,histfunc='sum'
                ,orientation='h'
                ,marker_color='#523E89'
                ,name='October'
                ,opacity=0.6)
            ,row=1
            ,col=2)    

fig_add = fig.update_xaxes(
            autorange="reversed"
            ,row=1
            ,col=1)

fig_add = fig.update_yaxes(
            visible=False
            ,row=1
            ,col=2)

fig_add.show()

我的图表: My chart

python plotly visualization
1个回答
0
投票

两个图之间的空白区域在概念上是图的面积和子图的大小,可以在

print(fig.layout)
中看到。因此,更改该区域值将产生预期结果。

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
        rows=1
        ,cols=2
        ,vertical_spacing=0
)

fig_add = fig.add_trace(
            go.Histogram(
                x=tornado_df.loc[tornado_df['month'] == 'September', 'Sales']
                ,y=tornado_df.loc[tornado_df['month'] == 'September', 'State']
                ,histfunc='sum'
                ,orientation='h'
                ,marker_color='#D95002'
                ,name='September'
                ,opacity=0.6)
            ,row=1
            ,col=1
)

fig_add = fig.add_trace(
            go.Histogram(
                x=tornado_df.loc[tornado_df['month'] == 'October', 'Sales']
                ,y=tornado_df.loc[tornado_df['month'] == 'October', 'State']
                ,histfunc='sum'
                ,orientation='h'
                ,marker_color='#523E89'
                ,name='October'
                ,opacity=0.6)
            ,row=1
            ,col=2
)    

fig_add = fig.update_xaxes(
            autorange="reversed"
            ,row=1
            ,col=1)

fig_add = fig.update_yaxes(
            visible=False
            ,row=1
            ,col=2)

fig.update_layout(xaxis=dict(domain=[0.0, 0.45]), xaxis2=dict(domain=[0.45, 0.90]))
fig.update_layout(legend=dict(orientation='h', xanchor='center', x=0.45))

fig_add.show()

© www.soinside.com 2019 - 2024. All rights reserved.