Customizing x-axis for Tornado chart in Plotly

Ars

How can i set the single start point for two mirror axis? I have built a graph with 'make_subplot' function. But I get an empty space in the center. I use the 'Sample Superstore' dataset from Tableau data collection.

My code:

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: My chart

r-beginners

The blank space between the two graphs is plotly the area of the graph and the size of the subplot in concept, which can be seen in print(fig.layout). So, changing that region value will produce the intended result.

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()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive