3

我正在尝试使用 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')

在这里,我为我的列表创建了一个变量,我认为应该定义直方图的 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()

直方图

它创建了一个图形位,x 的范围从 0 到 0.2050,y 的范围从 -0.04 到 0.04。和我想象的完全不同。我谷歌搜索了很长一段时间,但似乎无法找到我的具体问题的答案。

已经,谢谢你们的帮助。

4

1 回答 1

5

There are a few approaches to achieve the desired results here. For example, you can change the xaxis limits after you have plotted the histogram, or adjust the range over which the bins are created.

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

Change the xaxis limits (the bins are still created over the range of your data):

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

enter image description here

Create the bins over the range 0 to 1:

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

enter image description here

Since the range for the bins is larger, you now need to use more bins if you want to have the same bin width as when adjusting the xlim:

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

enter image description here

于 2017-10-28T19:51:11.327 回答