2017-10-28 117 views
2

我试图创建一个带seaborn的直方图,其中bin从0开始并转到1.但是,只有在0.22到0.34范围内的日期。为了更好地呈现数据,我希望空白空间更具视觉效果。扩展seaborn直方图中bin的范围

创建我的片材

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', sheetname='IvT') 

在这里,我创建了一个变量为我的列表和一个我认为应该定义直方图的区间范围内。

st = pd.Series(df['Short total']) 
a = np.arange(0, 1, 15, dtype=None) 

和直方图本身看起来像这样

sns.set_style("white") 
plt.figure(figsize=(12,10)) 
plt.xlabel('Ration short/total', fontsize=18) 
plt.title ('CO3 In vitro transcription, Na+', fontsize=22) 

ax = sns.distplot(st, bins=a, kde=False) 

plt.savefig("hist.svg", format="svg") 
plt.show() 

Histogram

它创建了一个图表位X从0到0.2050和y在-0.04至0.04的范围内。与我所期望的完全不同。我谷歌搜索相当一段时间,但似乎无法找到我的具体问题的答案。

已经,谢谢你的帮助。

回答

1

这里有几种方法可以达到预期的效果。例如,您可以在绘制柱状图后更改x轴的极限,或者调整创建柱的范围。

import seaborn as sns 

# Load sample data and create a column with values in the suitable range 
iris = sns.load_dataset('iris') 
iris['norm_sep_len'] = iris['sepal_length']/(iris['sepal_length'].max()*2) 
sns.distplot(iris['norm_sep_len'], bins=10, kde=False) 

enter image description here

更改x轴限制值(箱仍然在你的数据范围创建):

ax = sns.distplot(iris['norm_sep_len'], bins=10, kde=False) 
ax.set_xlim(0,1) 

enter image description here

在范围0到创建箱1:

sns.distplot(iris['norm_sep_len'], bins=10, kde=False, hist_kws={'range':(0,1)}) 

enter image description here

由于对垃圾箱范围越大,你现在需要的,如果你想拥有同样的箱的宽度调整XLIM时,作为使用多个频段:

sns.distplot(iris['norm_sep_len'], bins=45, kde=False, hist_kws={'range':(0,1)}) 

enter image description here

+0

非常感谢你。那就是诀窍。有没有办法,我也可以使用下面的参数来获得酒吧周围的边框? hist_kws = dict(edgecolor =“k”,linewidth = 2) – Jul

+0

@Jul'hist_kws'将参数从matplotlib发送到底层直方图函数。您可以通过阅读文档来查看所有可以传递的参数:'import matplotlib.pyplot as plt; ?plt.hist'在这种情况下,您希望将'histt​​ype'指定为''bar''。不要忘记接受和upvote这个答案它解决了你的问题。 –

+0

如果我将直方图的代码更改为ax = sns.distplot(st,bins = 34,kde = False,color ='#007b7f',histt​​ype ='bar',hist_kws = dict(edgecolor =“k”,linewidth = 2))它给了我一个错误消息,说:_distplot()有一个意外的关键字参数'histt​​ype'_。或者我必须更改matplotlib中的组织类型? – Jul