简体   繁体   中英

Seaborn distplot only whole numbers

How can I make a distplot with seaborn to only have whole numbers?

My data is an array of numbers between 0 and ~18. I would like to plot the distribution of the numbers.

Impressions
0      210
1     1084
2     2559
3     4378
4     5500
5     5436
6     4525
7     3329
8     2078
9     1166
10     586
11     244
12     105
13      51
14      18
15       5
16       3
dtype: int64

Code I'm using:

sns.distplot(Impressions,
              # bins=np.arange(Impressions.min(), Impressions.max() + 1),
              # kde=False,
             axlabel=False,
             hist_kws={'edgecolor':'black', 'rwidth': 1})
plt.xticks = range(current.Impressions.min(), current.Impressions.max() + 1, 1)

Plot looks like this:

在此处输入图片说明

What I'm expecting:

  • The xlabels should be whole numbers
  • Bars should touch each other
  • The kde line should simply connect the top of the bars. By the looks of it, the current one assumes to have 0s between (x, x + 1) , hence why the downward spike (This isn't required, I can turn off kde)

Am I using the correct tool for the job or distplot shouldn't be used for whole numbers?

For your problem can be solved bellow code,

import seaborn as sns # for data visualization
import numpy as np # for numeric computing
import matplotlib.pyplot as plt # for data visualization

arr = np.array([1,2,3,4,5,6,7,8,9])

sns.distplot(arr, bins = arr, kde = False)
plt.xticks(arr)
plt.show()

enter image description here

In this way, you can plot histogram using seaborn sns.distplot() function.

Note: Whatever data you will pass to bins and plt.xticks(). It should be an ascending order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM