Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to manually set the color of points in plotly express scatter plots?

https://plotly.com/python/line-and-scatter/ has many scatter plot examples, but not a single one showing you how to set all the points' colours within px.scatter:

# x and y given as DataFrame columns
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

I've tried adding colour = 'red' etc doesn't work. These examples only show you how to colour by some other variable.

In principle I could add another feature and set it all the same but that seems a bizzare way of accomplishing the task....

like image 621
RNs_Ghost Avatar asked Aug 14 '20 13:08

RNs_Ghost


People also ask

How do you specify colors in plotly?

FIGURE 27.1: Specifying a color in plotly. js' supported format. Just like in ggplot2, you'll have to specify a color in one of the following ways: A hexadecimal string of the form “#rrggbb” or “#rrggbbaa”.

How do you change the color of a line in plotly?

Using color_discrete_sequence Another method you can use to customize a line plot's color is the color_discrete_sequence parameter. This allows you to pass a list of color values that are assigned to each plot type. This should set the color in the list as the default line color for your plot.

How do you Plotly Express a scatter plot?

The syntax to create a scatterplot with Plotly Express is fairly simple. In the simple case, you simply call the function as px. scatter, provide the name of the dataframe you want to plot, and them map variables to the x and y axes. Note that this assumes that you've imported Plotly Express as px .

Is plotly customizable?

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments.


3 Answers

For that you may use the color_discrete_sequence argument.

fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])

This argument is to use a custom color paletter for discrete color factors, but if you are not using any factor for color it will use the first element for all the points in the plot.

More about discrete color palletes: https://plotly.com/python/discrete-color/

like image 167
Daniel R Avatar answered Oct 23 '22 23:10

Daniel R


As far as I understand your question, I would try to answer it.

The parameter 'color' only accepts the column names.
In your case, you can consider using update_traces()

import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.update_traces(marker=dict(
        color='red'))
fig.show()

Reference: https://plotly.com/python/marker-style/

like image 38
Shri Hari Avatar answered Oct 23 '22 22:10

Shri Hari


You do not have to add another feature to get what you want here. Thanks to Python's method chaining, you can just include .update_traces(marker=dict(color='red')) to manually assign any color of your choosing to all markers.

Plot:

enter image description here

Code:

# x and y given as DataFrame columns
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df,x="sepal_width",                         
                    y="sepal_length"
                 ).update_traces(marker=dict(color='red'))
fig.show()

like image 3
vestland Avatar answered Oct 24 '22 00:10

vestland