扩展 seaborn 直方图中的 bin 范围
Extending the range of bins in seaborn histogram
我正在尝试使用 seaborn 创建直方图,其中 bins 从 0 开始到 1。但是,只有 0.22 到 0.34 范围内的日期。我想要空 space 更多的视觉效果,以更好地呈现数据。
我用
创建我的 sheet
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')
我在这里为我的列表创建了一个变量,我认为应该定义直方图的 bin 范围。
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。与我的预期完全不同。我 google 搜索了很长时间,但似乎找不到我的具体问题的答案。
已经,谢谢你们的帮助。
这里有几种方法可以达到预期的效果。例如,您可以在绘制直方图后更改 x 轴限制,或调整创建 bin 的范围。
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)
更改 x 轴限制(仍会在您的数据范围内创建 bin):
ax = sns.distplot(iris['norm_sep_len'], bins=10, kde=False)
ax.set_xlim(0,1)
在 0 到 1 的范围内创建 bin:
sns.distplot(iris['norm_sep_len'], bins=10, kde=False, hist_kws={'range':(0,1)})
由于 bins 的范围更大,如果你想拥有与调整 xlim 时相同的 bin 宽度,你现在需要使用更多的 bins:
sns.distplot(iris['norm_sep_len'], bins=45, kde=False, hist_kws={'range':(0,1)})
我正在尝试使用 seaborn 创建直方图,其中 bins 从 0 开始到 1。但是,只有 0.22 到 0.34 范围内的日期。我想要空 space 更多的视觉效果,以更好地呈现数据。
我用
创建我的 sheetimport 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')
我在这里为我的列表创建了一个变量,我认为应该定义直方图的 bin 范围。
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。与我的预期完全不同。我 google 搜索了很长时间,但似乎找不到我的具体问题的答案。
已经,谢谢你们的帮助。
这里有几种方法可以达到预期的效果。例如,您可以在绘制直方图后更改 x 轴限制,或调整创建 bin 的范围。
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)
更改 x 轴限制(仍会在您的数据范围内创建 bin):
ax = sns.distplot(iris['norm_sep_len'], bins=10, kde=False)
ax.set_xlim(0,1)
在 0 到 1 的范围内创建 bin:
sns.distplot(iris['norm_sep_len'], bins=10, kde=False, hist_kws={'range':(0,1)})
由于 bins 的范围更大,如果你想拥有与调整 xlim 时相同的 bin 宽度,你现在需要使用更多的 bins:
sns.distplot(iris['norm_sep_len'], bins=45, kde=False, hist_kws={'range':(0,1)})