改变seaborn水平条形图的颜色

问题描述 投票:1回答:1

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

%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)  
python
1个回答
0
投票

您可以使用参数:seaborn.barplot

color: matplotlib color, optional

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

用于可视化示例示例:Horizontal bar plots

这是一个没有颜色的例子,所以结果是“彩虹”:

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

现在我们指定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**

© www.soinside.com 2019 - 2024. All rights reserved.