Skip to content

Bug when using categorical axes and colors in plotly.express #3198

Description

@camold

According to the documentation:

By default, Plotly Express lays out categorical data in the order in which it appears in the underlying data.

However, when using categorical axes in conjunction with colors, things do not show up in the order of the underlying data anymore.

import plotly.express as px
pdata_x = ["A","B",None,"B","C",None,"C","D", None, "D", "E", None]
pdata_y = [1,1,None,2,2,None,3,3,None,4,4,None]
pdata_color = [1,1,1,2,2,2,2,2,2,1,1,1]
px.line(x=pdata_x, y=pdata_y, color=pdata_color).show()

Output is
grafik

When not(!) using colors, we would get the correct output:

px.line(x=pdata_x, y=pdata_y ).show()

grafik

For anyone who comes across this problem, a temporary fix is explicitely specifying the category order:

px.line(x=pdata_x, y=pdata_y, color=pdata_color, category_orders={"x":["A","B","C","D"]}).show()

grafik

plotly==4.14.3
Python 3.8.0 (default, Feb 25 2021, 22:10:10)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions