Plotly龙卷风图X轴自定义:双镜像轴起始点统一设置
解决Plotly龙卷风图镜像轴起始点统一问题
我用Plotly的make_subplot函数制作龙卷风图,采用Tableau的Sample Superstore数据集,当前图表中间存在空白区域,请问如何为两个镜像轴设置统一的起始点?
我的代码:
import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots # 假设df是已加载的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()
我的图表:
解决方法
要让两个镜像轴对齐零点、消除中间空白,需要做以下调整:
计算统一的轴范围
先找出两个月份销售额的最大值,以此作为左右轴的对称范围:max_sales = max(tornado_df['sales'].max(), abs(tornado_df['sales'].min()))设置固定轴范围并对齐零点
对左右两个x轴分别指定范围,左轴范围设为[-max_sales, 0](自带反转效果),右轴范围设为[0, max_sales],同时关闭自动缩放:fig.update_xaxes(range=[-max_sales, 0], autorange=False, row=1, col=1) fig.update_xaxes(range=[0, max_sales], autorange=False, row=1, col=2)消除子图间距
在make_subplots中添加horizontal_spacing=0,让两个子图紧贴在一起:fig = make_subplots( rows=1, cols=2, vertical_spacing=0, horizontal_spacing=0 )
修改后的完整代码
import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots # 假设df是已加载的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) # 计算最大销售额,用于对称轴范围 max_sales = max(tornado_df['sales'].max(), abs(tornado_df['sales'].min())) fig = make_subplots( rows=1, cols=2, vertical_spacing=0, horizontal_spacing=0 # 消除子图间距 ) 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_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.update_xaxes(range=[-max_sales, 0], autorange=False, row=1, col=1) fig.update_xaxes(range=[0, max_sales], autorange=False, row=1, col=2) fig.update_yaxes(visible=False, row=1, col=2) fig.show()
这样修改后,左右轴的零点会完全对齐,中间空白区域消失,形成标准的龙卷风图样式。
内容的提问来源于stack exchange,提问作者Ars
相关产品推荐
相关产品推荐

