当前位置: 代码迷 >> python >> 更改Seaborn水平条形图的颜色
  详细解决方案

更改Seaborn水平条形图的颜色

热度:64   发布时间:2023-06-19 09:31:04.0

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

%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)  

您可以使用参数:

color: matplotlib color, optional

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

用于可视化示例示例:

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

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' #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')

  相关解决方案