Plotly: How to change the colorscheme of a plotly express scatterplot?

wundermahn :

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 below, that produces a nice scatterplot:

import plotly.graph_objs as go, pandas as pd, plotly.express as px
df = pd.read_csv('iris.csv')
fig = px.scatter(df, x='sepal_length', y='sepal_width',
              color='species', marker_colorscale=px.colors.sequential.Viridis)
fig.show()

enter image description here

However, I want to try and change the colorscheme, i.e., the colors presented for each species.

I have read:

But can not get the colors to change.

Trying:

fig = px.scatter(df, x='sepal_length', y='sepal_width',
              color='species', marker_colorscale=px.colors.sequential.Viridis)

yields:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-78a9d58dce23> in <module>
      2 # https://plotly.com/python/line-and-scatter/
      3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
----> 4               color='species', marker_colorscale=px.colors.sequential.Viridis)
      5 fig.show()

TypeError: scatter() got an unexpected keyword argument 'marker_colorscale'

Trying

Trying:

fig = px.scatter(df, x='sepal_length', y='sepal_width',
              color='species', continuous_colorscale=px.colors.sequential.Viridis)

yields:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-78a9d58dce23> in <module>
      2 # https://plotly.com/python/line-and-scatter/
      3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
----> 4               color='species', continuous_colorscale=px.colors.sequential.Viridis)
      5 fig.show()

TypeError: scatter() got an unexpected keyword argument 'continuous_colorscale'

How can I change the colors used in a plotly visualization?

vestland :

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:

enter image description here

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:

enter image description here

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:

enter image description here

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:

enter image description here

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 becauce 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 with px.colors.qualitative

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386491&siteId=1