Anastasia Vargas
Anastasia Vargas

Reputation: 65

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

Upvotes: 5

Views: 10342

Answers (3)

Ryan Dorrill
Ryan Dorrill

Reputation: 486

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

Upvotes: 2

Maria
Maria

Reputation: 397

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.

Upvotes: 9

Ali Maleki
Ali Maleki

Reputation: 31

The most important function for working with color palettes is, aptly, color_palette(). This function provides an interface to most of the possible ways that one can generate color palettes in seaborn. And it’s used internally by any function that has a palette argument.For example:

sns.color_palette("tab10")

Upvotes: 1

Related Questions