扩展seaborn直方图中的箱子范围

时间:2017-10-28 12:20:13

标签: python histogram seaborn bins

我正在尝试使用seaborn创建一个直方图,其中二进制文件从0开始并转到1.但是,只有0.22到0.34范围内的日期。我希望空白空间更具视觉效果,以便更好地呈现数据。

我用

创建了我的工作表
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 个答案:

答案 0 :(得分:2)

这里有一些方法可以达到预期的效果。例如,您可以在绘制直方图后更改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

相关问题