使用 plotly.py 构建图表时 X 轴上的值顺序错误

Wrong order of values on X axes when build charts with groups using plotly.py

我的数据中的行由三列组成:版本、配置和值。我想要两条线代表我图表上的配置,以显示值(y 轴)对版本(x 轴)的依赖性。只要每个配置(组)在 x 轴上具有相同的一组值,一切都会完美无缺:

import plotly.express as px
import pandas

rows = [
    ['1',  'a', 4],
    ['1',  'b', 3],
    ['2',  'a', 6],
    ['2',  'b', 3],
    ['3',  'a', 6],
    ['3',  'b', 7],
]

df = pandas.DataFrame(columns=['version', 'config',  'value'],
                        data=rows)

fig = px.line(df,
            x='version',
            y='value',
            color='config',
            line_group='config'
            )

fig.write_html("charts.html")

charts.html

当一个类别在 x 轴上没有某些值时,问题就开始了:

rows = [
    ['1',  'a', 4],
    ['1',  'b', 3],
    # ['2',  'a', 6],
    ['2',  'b', 3],
    ['3',  'a', 6],
    ['3',  'b', 7],
]

如您所见,图表上的版本顺序错误:charts.html

这里的问题是我们根据输入数据中的第一个类别(a 在我们的例子中)对 x 轴上的值进行排序。例如,当从 b 类别中删除行时,我看到了正确的顺序。

在我的例子中使用字符串作为基本版本,一个数字版本只是为了简化示例。

问题是如何根据所有类别中的值对 x 轴进行排序?

运行 您在 Google Colab 笔记本中的代码,这创建了具有正确排序轴的结果图表。

结果图:

我认为这是由包裹差异引起的。这是我的 colab notebook 中导入的包:

pandas                        1.1.5              
pandas-datareader             0.9.0              
pandas-gbq                    0.13.3             
pandas-profiling              1.4.1
plotly                        4.4.1

也许尝试 运行 你在 Colab 中的代码和/或确保包版本匹配,我认为 plotly 版本是最有可能的罪魁祸首。

我的解决方案是使用 category_orders 参数:

fig = px.line(df,
            x='version',
            y='value',
            color='config',
            line_group='config',
            category_orders={'version': df["version"]}
            )

category_orders to override the default category ordering behaviour, which is to use the order in which the data appears in the input. category_orders accepts a dict whose keys are the column name to reorder and whose values are a list of values in the desired order. These orderings apply everywhere categories appear: in legends, on axes, in bar stacks, in the order of facets, in the order of animation frames etc.

来源:https://plotly.com/python/styling-plotly-express/