如何计算matplotlib/seaborn中间隔的绘图列值?

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

我有一列值,我必须绘制像 <0.99,1 到 1.99,2 到 3.99 和 4> 这样的值 我试过下面的代码

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)

但它给出的输出是这样的

如何从 (-inf,0.99),(1,1.99),(2,3.99) 和 (4,inf) 制作柱状图?

如果你真的想排除部分范围,那么你必须自己组织直方图:

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()

示例输出:

最后一个 bin 实际上包含 np.inf,但您将设法将 x 标签更改为 [4.0, inf]