Gavin
Gavin

Reputation: 1521

change color for seaborn horizontal bar chart

I am trying to plot a horizontal bar chart. It works but the color is in rainbow style. How to change the color for each bar to the same color?

%pyspark
import seaborn as sns
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
plt.clf()

sns.set_context("notebook", font_scale=0.5)

# plot barchart by x axis and use different color for day
sns.barplot(x = "scaled_importance", 
        y = "variable", 
        data = best_gbm_varimp[:10],
        orient = "h")

show(plt)  

Upvotes: 5

Views: 11458

Answers (1)

n1tk
n1tk

Reputation: 2490

You can use the parameter: seaborn.barplot

color: matplotlib color, optional

Color for all of the elements, or seed for a gradient palette.

Used for visualization the sample example: Horizontal bar plots

Here is an example without color so result is "rainbow":

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(6, 15))

# Load the example car crash dataset
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)


# Plot the crashes where alcohol was involved
sns.set_color_codes("muted")
sns.barplot(x="alcohol", y="abbrev", 
            data=crashes,
            label="Alcohol-involved", 
            orient = "h")

sample output with all the colors

Now we specify the color='b' #blue:

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(6, 15))

# Load the example car crash dataset
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)


# Plot the crashes where alcohol was involved
sns.set_color_codes("muted")
sns.barplot(x="alcohol", y="abbrev", 
            data=crashes,
            label="Alcohol-involved", 
            orient = "h",
            color='b')

with bars colored **blue**

Upvotes: 3

Related Questions