Mikhail Falaleev
Mikhail Falaleev

Reputation: 21

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

Rows in my data consist of three columns: version, configuration, and value. I want to have two lines which represent configurations on my chart to show dependency of value (y axis) on version (x axis). Everything works perfect as long as every configuration (group) have the same set of values on x axis:

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

Problems start when one category does not have some value on x axis:

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

As you can see we have versions in the wrong order on the chart: charts.html

The problem here is that we order values on x axis based on first category in input data (a in our case). For example, when remove row from b category, I see correct order.

Using string as a version in essential in my case, one digit version is just for simplicity of the example.

Question is how to order x axis based on values in all categories?

Upvotes: 0

Views: 1616

Answers (2)

Ryan
Ryan

Reputation: 1

Running your code within a Google Colab notebook, this created the results chart with the correctly ordered axis.

Results chart: enter image description here

I would assume this is caused by package discrepancies. This is what my colab notebook had for the imported packages:

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

Perhaps try running your code within Colab and or ensuring the package versions match, I would think plotly version is the most likely culprit.

Upvotes: 0

Mikhail Falaleev
Mikhail Falaleev

Reputation: 21

My solution is to use category_orders argument:

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.

Source: https://plotly.com/python/styling-plotly-express/

Upvotes: 2

Related Questions