更改seaborn水平条形图的颜色

Gav*_*vin 5 python

我正在尝试绘制水平条形图。它有效,但颜色是彩虹风格的。如何将每个条形的颜色更改为相同的颜色?

%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)  
Run Code Online (Sandbox Code Playgroud)

n1t*_*1tk 3

您可以使用参数:seaborn.barplot

color: matplotlib color, optional

Color for all of the elements, or seed for a gradient palette.
Run Code Online (Sandbox Code Playgroud)

用于可视化示例:水平条形图

这是一个没有颜色的示例,因此结果是“彩虹”:

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")
Run Code Online (Sandbox Code Playgroud)

具有所有颜色的示例输出

现在我们指定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')
Run Code Online (Sandbox Code Playgroud)

条形颜色为**蓝色**