I am trying to work with plotly, specifically ploty express, to build a few visualizations.
One of the things I am building is a scatterplot
I have some code bel
Generally, changing the color scheme for a plotly express figure is very straight-forward. What's causing the problems here is the fact that species
is a categorical variable. Continuous or numerical values are actually easier, but we'll get to that in a bit.
For categorical values, using color_discrete_map
is a perfectly valid, albeit cumbersome approach. I prefer using the keyword argument continuous_colorscale
in combination with px.colors.qualitative.Antique
, where Antique
can be changed to any of the discrete color schemes available in plotly express. Just run dir(px.colors.qualitative)
to see what are available to you in the plotly version you are running:
['Alphabet',
'Antique',
'Bold',
'D3',
'Dark2',
'Dark24',
'G10',......]
Code 1:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="species", color_discrete_sequence=px.colors.qualitative.Antique)
fig.show()
Plot 1:
So what about continuous variables?
Consider the following snippet:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)
fig.show()
Running this will produce this plot:
You can change the colors to any other theme available under dir(px.colors.sequential)
, for example color_continuous_scale=px.colors.sequential.Inferno
, and get this plot:
What's possibly causing confusion here, is that setting color='species
, and keeping color_continuous_scale=px.colors.sequential.Inferno
will give you this plot:
The figure now jumps straight back to using the default plotly colors, without giving you any warning about color_continuous_scale=px.colors.sequential.Inferno
not having an effect.
This is because species
is a categorical variable with these different values : ['setosa', 'versicolor', 'virginica']
, so color_continuous_scale
is simply ignored. For color_continuous_scale
to take effect you'll have to use a numerical value, like sepal_length = [5.1, 4.9, 4.7, 4.6, 5. , 5.4, ...]
And this brings us right back to my initial answer for categorical values:
Use the keyword argument
continuous_colorscale
in combination withpx.colors.qualitative