在 seaborn distplot 中增加 bin 之间的 space
Increasing space between bins in seaborn distplot
所以我有这个可能很简单的问题。我使用 seaborn 从 excel 文件中的数据创建了一个直方图。为了更好的可视化,我想在 bars/bins 之间有一些 space。这可能吗?
我的代码如下所示
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')
第二个问题虽然有点偏离主题,但我如何让图表标题中的指数实际提升?
谢谢!
matplotlib hist
函数有一个参数 rwidth
rwidth
: scalar or None, optional
The relative width of the bars as a fraction of the bin width.
您可以通过 hist_kws
参数在 distplot
中使用它。
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()
对于 Seaborn >= 0.11,使用 shrink
参数。它通过此参数缩放每个条相对于 binwidth 的宽度。其余的将是空的 space.
文档:https://seaborn.pydata.org/generated/seaborn.histplot.html
编辑:
OP 最初询问的是 sns.distplot()
,但是在当前版本 >=0.11
中已弃用 sns.histplot
或 sns.displot()
。由于 OP 正在生成直方图,因此 hist 模式中的 histplot
和 displot
都将采用 shrink
发布我的答案后,我意识到我回答的问题与所问的相反。我在尝试弄清楚如何删除条形之间的 space 时发现了这个问题。我几乎删除了我的答案,但如果其他人偶然发现这个问题并试图删除 seaborn 的 histplot 中条形图之间的 space,我暂时保留它。
感谢@miro 提供 Seaborn 的 updated documentation,我发现 element='step'
对我有用。根据您的具体需求,element='poly'
可能就是您所追求的。
我使用 'step' 的实现:
fig,axs = plt.subplots(4,2,figsize=(10,10))
i,j = 0,0
for col in cols:
sns.histplot(df[col],ax=axs[i,j],bins=100,element='step')
axs[i,j].set(title="",ylabel='Frequency',xlabel=labels[col])
i+=1
if i == 4:
i = 0
j+=1
我使用 'poly' 的实现:
fig,axs = plt.subplots(4,2,figsize=(10,10))
i,j = 0,0
for col in cols:
sns.histplot(df[col],ax=axs[i,j],bins=100,element='poly')
axs[i,j].set(title="",ylabel='Frequency',xlabel=labels[col])
i+=1
if i == 4:
i = 0
j+=1
所以我有这个可能很简单的问题。我使用 seaborn 从 excel 文件中的数据创建了一个直方图。为了更好的可视化,我想在 bars/bins 之间有一些 space。这可能吗?
我的代码如下所示
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')
第二个问题虽然有点偏离主题,但我如何让图表标题中的指数实际提升?
谢谢!
matplotlib hist
函数有一个参数 rwidth
rwidth
: scalar or None, optional
The relative width of the bars as a fraction of the bin width.
您可以通过 hist_kws
参数在 distplot
中使用它。
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()
对于 Seaborn >= 0.11,使用 shrink
参数。它通过此参数缩放每个条相对于 binwidth 的宽度。其余的将是空的 space.
文档:https://seaborn.pydata.org/generated/seaborn.histplot.html
编辑:
OP 最初询问的是 sns.distplot()
,但是在当前版本 >=0.11
中已弃用 sns.histplot
或 sns.displot()
。由于 OP 正在生成直方图,因此 hist 模式中的 histplot
和 displot
都将采用 shrink
发布我的答案后,我意识到我回答的问题与所问的相反。我在尝试弄清楚如何删除条形之间的 space 时发现了这个问题。我几乎删除了我的答案,但如果其他人偶然发现这个问题并试图删除 seaborn 的 histplot 中条形图之间的 space,我暂时保留它。
感谢@miro 提供 Seaborn 的 updated documentation,我发现 element='step'
对我有用。根据您的具体需求,element='poly'
可能就是您所追求的。
我使用 'step' 的实现:
fig,axs = plt.subplots(4,2,figsize=(10,10))
i,j = 0,0
for col in cols:
sns.histplot(df[col],ax=axs[i,j],bins=100,element='step')
axs[i,j].set(title="",ylabel='Frequency',xlabel=labels[col])
i+=1
if i == 4:
i = 0
j+=1
我使用 'poly' 的实现:
fig,axs = plt.subplots(4,2,figsize=(10,10))
i,j = 0,0
for col in cols:
sns.histplot(df[col],ax=axs[i,j],bins=100,element='poly')
axs[i,j].set(title="",ylabel='Frequency',xlabel=labels[col])
i+=1
if i == 4:
i = 0
j+=1