python - 使用seaborn深色主题并删除条形图中条形之间的间隙

标签 python matplotlib themes seaborn bar-chart

我想要一个条形图之间没有间隙的条形图,因此我在 plt.bar 中使用 width=1,并且它有效。

# IMPORTS
import seaborn as sns
import pandas as pd
import datetime
import matplotlib.pyplot as plt

# DUMMY DATAFRAME
my_dict = {'date': {0: datetime.date(2021, 5, 18),  1: datetime.date(2021, 5, 19),  2: datetime.date(2021, 5, 20),  
  3: datetime.date(2021, 5, 21),  4: datetime.date(2021, 5, 22),  5: datetime.date(2021, 5, 18),
  6: datetime.date(2021, 5, 19),  7: datetime.date(2021, 5, 20),  8: datetime.date(2021, 5, 21),  
  9: datetime.date(2021, 5, 22)},
 'campaign': {0: 'A',  1: 'A',  2: 'A',  3: 'A',  4: 'A',  5: 'B',  6: 'B',  7: 'B',  8: 'B',  9: 'B'},
 'metric': {0: 'cost',  1: 'cost',  2: 'cost',  3: 'cost',  4: 'cost',  5: 'cost',  6: 'cost',  7: 'cost',
  8: 'cost',  9: 'cost'},
 'value': {0: 2.01,  1: 2.62,  2: 1.95,  3: 2.03,  4: 2.13,  5: 38.67,  6: 45.86,  7: 42.14,  8: 37.9,
  9: 34.23}}

df = pd.DataFrame(my_dict)

# FACETGRID PLOT
g = sns.FacetGrid(df, col='campaign', hue='metric', 
                  col_wrap=2, palette = ['goldenrod','steelblue'],height=5)

g = g.map(plt.bar, 'date', 'value',alpha = 0.6, width=1)

for ax in g.axes.ravel():
    ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
plt.show()

enter image description here

但我也想要深色背景,因此我在使用 FaceGrid 之前添加 sns.set_theme(style="dark")。现在,间隙(条之间的白色边缘线)又回来了。

enter image description here

我可以消除间隙并仍然使用深色主题吗?

最佳答案

您可以使用 set_style 并覆盖 patch.edgecolor (请参阅 overriding elements of the seaborn styles )。

sns.set_style('dark', {"patch.edgecolor": 'None'})

输出: enter image description here

编辑:

或者与set_theme类似:

sns.set_theme(style='dark', rc={"patch.edgecolor": 'None'})

关于python - 使用seaborn深色主题并删除条形图中条形之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68469485/

相关文章:

python - 如何从 S3 存储桶 SRC 文件

python - 如何在 Django 中对日期和时间执行聚合?

python - 如何将图例标签添加为条形图注释?

python - API 和 Python 的身份验证

python - 为什么在递归情况下出现 "Function not Defined"错误?

python - Matplotlib:如何在 x 轴上绘制一 strip 有分类数据的线?

Python - 用散点图堆叠两个直方图

android - 在 Android 4 中更改首选项摘要文本颜色

Emacs Lisp 定义一个函数,其中嵌入了 after-make-frame-functions

html - 这种布局可以用 div 吗?