增加seaborn distplot中的垃圾箱之间的空间

Jul*_*Jul 3 python matplotlib histogram bins seaborn

所以我有这个可能是一个简单的问题.我用seaborn的excel文件中的数据创建了一个直方图.为了更好的可视化,我想在条/箱之间留出一些空间.那可能吗?

我的代码看起来如下

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')
Run Code Online (Sandbox Code Playgroud)

第二个问题虽然有点偏离主题,但我如何让图表标题中的指数真正被提升?

谢谢!

mir*_*iro 9

对于 Seaborn >= 0.11,使用shrink参数。它通过此参数缩放每个条形相对于 binwidth 的宽度。其余的将是空的。

文档:https ://seaborn.pydata.org/ generated/seaborn.histplot.html

编辑:OP最初询问的是sns.distplot(),但是,它已被弃用,以支持sns.histplotsns.displot()在当前版本中>=0.11。由于 OP 正在生成直方图,因此histplotdisplot直方图模式下都将采用shrink


Imp*_*est 7

matplotlib hist函数有一个参数rwidth

rwidth:标量或无,可选
条形的相对宽度,作为条宽的一部分.

你可以在distplotvia hist_kws参数中使用它.

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()
Run Code Online (Sandbox Code Playgroud)

hist_kws