Get color from category in Plotly Coordinates Plot

Question:

I have a dataframe that looks like this:
enter image description here

I want to crate a Coordinates Plot:

fig = px.parallel_coordinates(newdf, 
                          color="Type_enc", 
                          dimensions=["Attack","Defense","HP","Sp.Attack","Sp.Defense","Speed"],
                         color_continuous_scale=[[0, 'green'], [1, 'red']],
                         color_continuous_midpoint=0.5,
                         title ="Pokemon Stats by Type")
fig.show()

And the result looks like this:
enter image description here

How do I change to legend to a discrete scale? So that instead of the encoded number, I just get "Fire" and "Water"? I tried changing

color="Type"

but this results in an error

Invalid element(s) received for the 'color' property of parcoords.line
    Invalid elements include: ['FIRE', 'FIRE', 'FIRE', 'FIRE', 'FIRE', 'WATER', 'WATER', 'WATER', 'WATER', 'FIRE']

Thank you!

Asked By: Justin

||

Answers:

Following the comments, that should do the trick:

fig = px.parallel_coordinates(newdf, 
                          color="Type_enc", 
                          dimensions=["Attack","Defense","HP","Sp.Attack","Sp.Defense","Speed"],
                          color_continuous_scale=[(0.0, "green"), (0.5, "green"), 
                                                 (0.5, "red"), (1.0, "red")],
                          title ="Pokemon Stats by Type")

fig.update_layout(coloraxis_colorbar=dict(
    title="Type",
    tickvals=[0,1],
    ticktext=["Water","Fire"], # You might want to change the order here!
    ))

fig.show()
Answered By: Mat.B
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.