Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change plot color seaborn package

I would like to change colors in this plot, it visualizes data properly but as you can see it isn't easy to read because all this colors are very similar (7 classes). Is there simple way to do it? Code for generating plot:

sns.pairplot(data, kind="scatter", hue = "Class")

Piece of plot

like image 300
Anastasia Vargas Avatar asked Sep 01 '26 21:09

Anastasia Vargas


2 Answers

You can use the optional argument palette, such as in (see here):

sns.pairplot(data, kind="scatter", hue = "Class", palette = "Paired")

In this case, I chose the color palette "Paired", but there are many others. You could also use:

sb.set_palette("dark")
sns.pairplot(data, kind="scatter", hue = "Class")

You can learn more about the available color palettes in the Seaborn site, https://seaborn.pydata.org/tutorial/color_palettes.html.

like image 90
Maria Avatar answered Sep 04 '26 11:09

Maria


As is mentioned in some other answers, Seaborn doesn't always use the color palette setting when plotting. For example, when using histplot for a 2D scatter plot, I was always stuck with the rocket color palette, which is a boring blue. What I wanted was color scaling based on the density in each 2D bin. One can fix this with the cmap option. Here's an example using housing data that creates a pretty rainbow colormap.

import pandas as pd
import seaborn as sns
url = 'data/ames-housing-dataset.zip'
housing = pd.read_csv(url, engine='pyarrow', dtype_backend='pyarrow')

sns.histplot(
    housing, x="1st Flr SF", y="SalePrice",
    bins=30, discrete=(False, False), log_scale=(False, False),cbar=True,
    hue_norm=True, cmap="viridis"
)

Below is the result. I hope that's helpful.

A seaborn 2D histplot with color scaling based on density of data points

like image 29
Ryan Dorrill Avatar answered Sep 04 '26 12:09

Ryan Dorrill