如何设置两个镜像轴的单个起点?我已经用‘make_subploy’功能构建了一个图形.但我在中间有个空位.我使用的是来自Tableau data collection的‘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: My chart

推荐答案

两张图之间的空白处是图的面积和概念上的子图的大小,可以在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()

enter image description here

Python相关问答推荐

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

返回nxon矩阵的diag元素,而不使用for循环

更改matplotlib彩色条的字体并勾选标签?

将整组数组拆分为最小值与最大值之和的子数组

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

如何将双框框列中的成对变成两个新列

运行终端命令时出现问题:pip start anonymous"

pandas滚动和窗口中有效观察的最大数量

如何从.cgi网站刮一张表到rame?

如何在python polars中停止otherate(),当使用when()表达式时?

我如何使法国在 map 中完全透明的代码?

为什么NumPy的向量化计算在将向量存储为类属性时较慢?'

Plotly Dash Creating Interactive Graph下拉列表

让函数调用方程

通过ManyToMany字段与Through在Django Admin中过滤

手动设置seborn/matplotlib散点图连续变量图例中显示的值

在Python中使用yaml渲染(多行字符串)

应用指定的规则构建数组

如何在Django查询集中生成带有值列表的带注释的字段?