Julien Massardier
Julien Massardier

Reputation: 1466

use seaborn dark theme and remove the gap between bars in a bar plot

I want a barplot without gap between the bars, so I use width=1 in plt.bar, and it works.

# 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

But I also want a dark background so I add sns.set_theme(style="dark") before using FaceGrid. Now, the gap, a white edge line between the bars, is back.

enter image description here

Can I eliminate the gap and still use a dark theme?

Upvotes: 2

Views: 1055

Answers (1)

BigBen
BigBen

Reputation: 49998

You could use set_style and override the patch.edgecolor (see overriding elements of the seaborn styles).

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

Output: enter image description here

EDIT:

Or similarly with set_theme:

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

Upvotes: 3

Related Questions