How can lines in Plotly chart be properly color-coded based on Category or Subcategory?

38 views Asked by At

I have a dataframe like this

    Category    Subcategory Date    Value
0   Category A  Subcategory A1  01/01/2016  31250
1   Category A  Subcategory A1  01/02/2016  32250
2   Category A  Subcategory A1  01/01/2017  32500
3   Category A  Subcategory A1  01/02/2017  33000
4   Category A  Subcategory A1  01/01/2018  32500

I'd like to create a Plotly line chart that represents the values for each Subcategory but I'd like the lines to be colored based on the Category. A pretty straightforward task, but I experience a behavior I cannot explain...

This is the code and the result:

fig = px.line(
    df,
    x="Date",
    y="Value",
    color="Category",
    # color="Subcategory",
)
fig.show()

Dataframe plotted with color="Category"

The same code with color set to Subcategory works fine:

fig = px.line(
    df,
    x="Date",
    y="Value",
    # color="Category",
    color="Subcategory",
)
fig.show()

Dataframe plotted with color="Subcategory"

How can I fix this? Thanks

0

There are 0 answers