plotly express in python repeats hue when making spaghetti plot

Viewed 587

I am trying to make a spaghetti plot using all the columns from a pandas dataframe using the following snippet :

import pandas as pd
import plotly.express as px

df_melt = df.melt('months', var_name='cols',  value_name='vals')
fig = px.line(df_melt, x='months' , y='vals' , color='cols',title="Symptoms frequency over the years 2018-19", labels={"vals":"Frequency","cols":"Keywords","months":"Timeline"})
fig.update_layout(font = dict(size = 10))

The csv looks something like this:

{'pathogen': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'flavivirus': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'swamp': {0: 5, 1: 6, 2: 9, 3: 19, 4: 39}, 
'virus': {0: 0, 1: 3, 2: 1, 3: 1, 4: 8}, 
'virus-bacteria': {0: 11, 1: 12, 2: 18, 3: 21, 4: 97}, 
'contagious': {0: 8, 1: 1, 2: 2, 3: 9, 4: 23}, 
'mosquito bite': {0: 1, 1: 1, 2: 6, 3: 3, 4: 7}, 
'agent': {0: 19, 1: 17, 2: 43, 3: 179, 4: 702}, 
'host': {0: 1, 1: 1, 2: 1, 3: 3, 4: 27}, 
'infected': {0: 0, 1: 1, 2: 0, 3: 1, 4: 4}, 
'bitten': {0: 0, 1: 1, 2: 0, 3: 2, 4: 12}, 
'bite': {0: 7, 1: 9, 2: 17, 3: 39, 4: 93}, 
'skin': {0: 34, 1: 43, 2: 38, 3: 154, 4: 269}, 
'sensitive': {0: 9, 1: 2, 2: 3, 3: 40, 4: 75}, 
'at risk': {0: 20, 1: 23, 2: 34, 3: 138, 4: 338}, 
'serotype': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'serotypes': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'protection': {0: 37, 1: 36, 2: 50, 3: 259, 4: 303}, 
'mucus membrane': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'spreaded': {0: 172, 1: 194, 2: 266, 3: 825, 4: 1424}, 
'vertebrate': {0: 0, 1: 0, 2: 0, 3: 0, 4: 1}, 
'human': {0: 707, 1: 676, 2: 961, 3: 2623, 4: 5270}, 
'mosquito': {0: 2, 1: 3, 2: 12, 3: 59, 4: 41}, 
'DEN-1': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den1': {0: 0, 1: 2, 2: 1, 3: 0, 4: 3}, 
'DEN-2': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den2': {0: 0, 1: 3, 2: 1, 3: 0, 4: 1}, 
'DEN-3': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den3': {0: 0, 1: 1, 2: 0, 3: 0, 4: 2}, 
'DEN-4': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 
'den4': {0: 0, 1: 3, 2: 0, 3: 1, 4: 3}, 
'DEN': {0: 1, 1: 4, 2: 2, 3: 4, 4: 5}, 
'months': {0: 'January18', 1: 'February18', 2: 'March18', 3: 'April18', 4: 'May18'}}

and my plot looks like this :spaghetti plot

i am using plotly 4.4.1 and plotly-express 0.4.1 .

the problem is the color for the keywords repeat , so it's tough for me make any interpretations from the graph. Is there any ways for me to stop the repeating? Also, not all the keywords are shown here, the label gets cropped when i save the plot. how do i remedy that? Thanks!

1 Answers

The default qualitative colorscale only has 10 items, as you can see if you type:

print(len(px.colors.qualitative.Plotly))

10

To use a differente scale, you can use the plotly express color_discrete_sequence argument. For example, to use the Dark24 scheme (this scheme has 24 colors), you can run:

px.line(..., color_discrete_sequence = px.colors.qualitative.Dark24)

You can pass your own color scheme as well if you pass a list:

px.line(..., color_discrete_sequence = ['#FF0000', ...])

You can also just append new colors to the default color scheme:

mycolorscheme = px.colors.qualitative.Plotly
mycolorscheme.append('#FF0000')
px.line(..., color_discrete_sequence = mycolorscheme)

You can see more on colors in the following links:

Related