简体   繁体   中英

How to count plot column value with interval in matplotlib/seaborn?

I have a column of value and I have to plot the value like <0.99,1 to 1.99,2 to 3.99 and 4> I tried the below code

data = pd.cut(TPA_Data_Details['cokumn name'], bins=[-np.inf,0.99,1,1.99,2,3.99,4,np.inf])
plt.figure(figsize=(17,15))
sns.countplot(data)

but it is giving the output like this在此处输入图像描述

how to make the bar from (-inf,0.99),(1,1.99),(2,3.99) and (4,inf)?

If you really want to exclude parts of the range, then you have to organize the histogram yourself:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

#generate sample data
np.random.seed(123)
n=100
df = pd.DataFrame({"A": np.random.random(n)*50 - 5})

#count the numbers per bin
vals, bins = np.histogram(df["A"], bins=[-np.inf,0.99,1,1.99,2,3.99,4,np.inf])

#plot and label only every other bar
plt.bar([f"[{i}, {j})" for i, j in zip(bins[::2], bins[1::2])], vals[::2])
plt.show()

Sample output: 在此处输入图像描述

The last bin actually includes np.inf but you will manage to change the x-label to [4.0, inf] .

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