Sijia Wang
Sijia Wang

Reputation: 1

Set the last bin of histogram to a range to include all large values Seaborn, Matplotlib

When there are outliers in a list of numbers, the last bins of its histogram are short and sparse. Thus, I want to have the last bin to use a range. For this example, it would be 10+ to include all values larger than 10.

import seaborn as sns
import matplotlib.pyplot as plt 
l = [1,2,3,4,5,1,2,3,6,7,8,6,4,5,3,2,8,3,1,2,3,5,6,7,15,20]
sns.displot(l, binwidth = 1, kde=True, rug=False)
plt.show()

The histogram of the list above

The histogram of the list above

I haven't figured out how to do it with seaborn. If matplotlib would work, that is also terrific. Many thanks in advance!

Upvotes: 0

Views: 2471

Answers (1)

mozway
mozway

Reputation: 262164

There is no direct way, but you can rework a bit the data to clip it to the desired value, and manually setting the xticks/xticklabels:

import seaborn as sns
import matplotlib.pyplot as plt 

l = [1,2,3,4,5,1,2,3,6,7,8,6,4,5,3,2,8,3,1,2,3,5,6,7,15,20]

MAX = 10 # max value after which the data will be grouped

f = sns.displot(np.clip(l, 0, MAX+1), binwidth = 1, kde=True, rug=False)
f.ax.set_xticks(np.arange(1, MAX+1)+0.5)
f.ax.set_xticklabels(np.arange(1, MAX).tolist()+[f'{MAX}+'])

output:

clipped histplot

Upvotes: 1

Related Questions