In what cases Seaborn makes histogram columns white? I use it Seaborn in Jupyter notebook:
JavaScript
x
4
1
import matplotlib.pyplot as plt
2
import seaborn as sns
3
sns.set()
4
Then I plot histogram using this function:
JavaScript
1
8
1
def plot_hist(data, xlabel, bins=None):
2
3
if not bins:
4
bins = int(np.sqrt(len(data)))
5
6
_= plt.xlabel(xlabel)
7
_= plt.hist(data, bins=bins)
8
As a result in some cases I have histograms with all blue columns or some blue and some white or only white columns. Please, see attached pictures.
How to make Seaborn always draw blue columns?
Advertisement
Answer
I believe the issue is that the edgecolor
for the histogram is white
, and as you increase the number of bins or decrease the width of the bars the edgecolor
begins to cover the facecolor
. You should be able to fix it by using a higher dpi,
JavaScript
1
6
1
# globally
2
from matplotlib import rcParams
3
rcParams['figure.dpi'] = 300
4
# or for only this figure
5
fig = plt.figure(dpi=300)
6
a thinner linewidth
,
JavaScript
1
6
1
# globally
2
from matplotlib import rcParams
3
rcParams['patch.linewidth'] = 0.5
4
# or for only this plot
5
_= plt.hist(data, bins=bins, linewidth=0.5)
6
or remove the outline altogether,
JavaScript
1
2
1
_= plt.hist(data, bins=bins, edgecolor='none')
2
Note that the global methods may need to be after sns.set()
as this may override them.