【问题标题】:Seaborn Boxplot with Same Color for All BoxesSeaborn 箱线图,所有箱体颜色相同
【发布时间】:2023-03-29 18:15:01
【问题描述】:

我正在使用 seaborn,并希望生成一个所有框都具有相同颜色的箱线图。出于某种原因,seaborn 为每个框使用不同的颜色,并且没有选项来停止此行为并为所有框设置相同的颜色。

如何强制 seaborn 对所有盒子使用相同的颜色?

fig, ax = plt.subplots(figsize=(10, 20))
sns.boxplot(y='categorical_var', x='numeric_var', ax=ax)

【问题讨论】:

    标签: python matplotlib colors seaborn boxplot


    【解决方案1】:

    使用color参数:

    import seaborn as sns
    tips = sns.load_dataset("tips")
    sns.boxplot(x="day", y="tip", data=tips, color="seagreen")
    

    【讨论】:

      【解决方案2】:

      制作自己的调色板并设置框的颜色,例如:

      import seaborn as sns
      import matplotlib.pylab as plt
      
      sns.set_color_codes()
      tips = sns.load_dataset("tips")
      pal = {day: "b" for day in tips.day.unique()}
      sns.boxplot(x="day", y="total_bill", data=tips, palette=pal)
      
      plt.show()
      

      另一种方法是遍历箱线图的艺术家,并为每个轴距离艺术家设置set_facecolor的颜色:

      ax = sns.boxplot(x="day", y="total_bill", data=tips)
      for box in ax.artists:
          box.set_facecolor("green")
      

      【讨论】:

      • 数据有点大。有没有办法在不运行数据的情况下做到这一点?
      • 单色完全不需要这些循环。
      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 2017-06-19
      相关资源
      最近更新 更多