简体   繁体   English

当 Y 包含负值和正值时,Plotly/Dash 在条形图中留下无法解释的空白

[英]Plotly/Dash leaving unexplained gaps in bar chart when Y contains negative and positive values

I am developing a web app that plots college basketball data scraped from KenPom.com (can be seen at kenpomgraphs.pythonanywhere.com).我正在开发一个 Web 应用程序,该应用程序绘制从 KenPom.com 抓取的大学篮球数据(可以在 kenpomgraphs.pythonanywhere.com 上看到)。 The first figure is a bar chart using px.bar(), and it is working as intended for the most part.第一个图是使用 px.bar() 的条形图,它在大多数情况下都按预期工作。 However, when I select conferences that include both positive and negative values for Y, the figure leaves a huge gap between the positive and negative bars.但是,当我选择包含 Y 的正值和负值的会议时,该数字在正条和负条之间留下了巨大的差距。

不良差距

If I copy the exact same code into a jupyter notebook (replacing return [fig1] with fig1.show() ) I get the desired output.如果我将完全相同的代码复制到 jupyter 笔记本中(用fig1.show()替换return [fig1] ),我会得到所需的输出。

无间隙

Here is the code that produces the graph along with the callbacks that provide the arguments.这是生成图形的代码以及提供参数的回调。 df is a dataframe that contains all the data. df是一个包含所有数据的数据框。 I filter it down to dff based on the arguments provided to the function.我根据提供给函数的参数将其过滤为dff I tried adding things like .reset_index() and .copy() thinking maybe it was a problem with the index or somehow referencing the unfiltered dataframe, but that has not changed the output.我尝试添加诸如.reset_index().copy()认为可能是索引有问题或以某种方式引用了未过滤的数据帧,但这并没有改变输出。

@app.callback(
    [Output('fig1', 'figure')],
    [Input('stat-column', 'value'),
    Input('number-teams', 'value'),
    Input('conf', 'value')]
)

def update_figure_1(stat_column_name, number_teams, conf):
    if stat_column_name == 'AdjD' or stat_column_name == 'OppD':
        dff = df.loc[df['Conf'].isin(conf)].sort_values(by=stat_column_name, ascending=True).head(number_teams).reset_index().copy()
    else: dff = df.loc[df['Conf'].isin(conf)].sort_values(by=stat_column_name, ascending=False).head(number_teams).reset_index().copy()

    fig1 = px.bar(data_frame=dff,
                x='Team',
                y=stat_column_name,
                color='Conf',
                color_discrete_map=COLORS
                )
    fig1.update_traces(hovertemplate='%{x}: %{y}')
    fig1.update_traces(marker=dict(line=dict(
                                            width=2,
                                            color='DarkSlateGrey')))
    fig1.update_yaxes(range=[min(dff[stat_column_name]) - abs((min(dff[stat_column_name])*.2)), max(dff[stat_column_name])*1.15])
    if stat_column_name == 'AdjD' or stat_column_name == 'OppD' or stat_column_name == 'Rk':
        fig1.update_layout(xaxis_categoryorder = 'total ascending')
    else: fig1.update_layout(xaxis_categoryorder = 'total descending')
    fig1.update_layout(transition_duration=500)
    fig1.update_yaxes(title=stat_column_name)
    fig1.update_xaxes(title='')
    return [fig1]

I am deploying the app using pythonanywhere.我正在使用 pythonanywhere 部署应用程序。 When I execute the code in a bash console I can confirm that dff only contains the rows I wish to display, so I can't understand why this gap is appearing, and why the same code does not produce the gap when run in a jupyter notebook.当我在 bash 控制台中执行代码时,我可以确认dff只包含我希望显示的行,所以我不明白为什么会出现这个间隙,以及为什么在 jupyter 中运行时相同的代码不会产生间隙笔记本。

I still don't know why the gap was there, but I was able to fix the problem by removing these lines of code:我仍然不知道为什么存在差距,但我能够通过删除这些代码行来解决问题:

    if stat_column_name == 'AdjD' or stat_column_name == 'OppD' or stat_column_name == 'Rk':
        fig1.update_layout(xaxis_categoryorder = 'total ascending')
    else: fig1.update_layout(xaxis_categoryorder = 'total descending')

and instead adding the argument而是添加参数

category_orders={'Team': list(dff['Team'])}

to px.bar()px.bar()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM