Increasing space between bins in seaborn distplot

Jul picture Jul · Nov 6, 2017 · Viewed 8.4k times · Source

So I have this, probably, simple question. I created a histogram from data out of an excel file with seaborn. Forbetter visualization, I would like to have some space between the bars/bins. Is that possible?

My code looks as followed

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'pdf')


df = pd.read_excel('test.xlsx')
sns.set_style("white")
#sns.set_style("dark")
plt.figure(figsize=(12,10))
plt.xlabel('a', fontsize=18)
plt.ylabel('test2', fontsize=18)

plt.title ('tests ^2', fontsize=22)


ax = sns.distplot(st,bins=34, kde=False, hist_kws={'range':(0,1), 'edgecolor':'black', 'alpha':1.0}, axlabel='test1')

A second question though a bit off topic would be, how I get the exponent in the title of the chart to actually be uplifted?

Thanks!

Answer

ImportanceOfBeingErnest picture ImportanceOfBeingErnest · Nov 6, 2017

The matplotlib hist function has an argument rwidth

rwidth : scalar or None, optional
The relative width of the bars as a fraction of the bin width.

You can use this inside the distplot via the hist_kws argument.

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

x = np.random.normal(0.5,0.2,1600)

ax = sns.distplot(x,bins=34, kde=False, 
                  hist_kws={"rwidth":0.75,'edgecolor':'black', 'alpha':1.0})

plt.show()

hist_kws