Seaborn Countplot系列无法识别输入

时间:2019-02-17 17:41:39

标签: python pandas seaborn

我正在尝试将一系列图绘制到水平条形图中,但是遇到了问题。

这是我的系列:

HARRASSMENT 2                     80149
CRIMINAL MISCHIEF & RELATED OF    62059
ASSAULT 3 & RELATED OFFENSES      61133
GRAND LARCENY                     54090
DANGEROUS DRUGS                   35407
OFF. AGNST PUB ORD SENSBLTY &     34810
ROBBERY                           23399
BURGLARY                          22536
FELONY ASSAULT                    21941
GRAND LARCENY OF MOTOR VEHICLE    14094
MISCELLANEOUS PENAL LAW           13307
DANGEROUS WEAPONS                 12090
Name: OFNS_DESC, dtype: int64```


This is what I already tried: 
sns.countplot(y="OFNS_DESC", data=b) , b is the name of my series variable

This is my error:

ValueError: Could not interpret input 'OFNS_DESC'



1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的数据,那么数字已经足够代表这些不同类别的计数-因此,您实际上只需要一个简单的条形图而不是countplot。

以下代码使用pandas / matplotlib和seaborn条形图函数来绘制数据:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

counts = [80149, 62059, 61133, 54090, 35407, 34810, 23399, 22536, 21941, 14094, 13307, 12090]
categories = ['HARRASSMENT 2', 'CRIMINAL MISCHIEF & RELATED OF', 'ASSAULT 3 & RELATED OFFENSES', 'GRAND LARCENY', 'DANGEROUS DRUGS', 'OFF. AGNST PUB ORD SENSBLTY &', 'ROBBERY', 'BURGLARY', 'FELONY ASSAULT', 'GRAND LARCENY OF MOTOR VEHICLE', 'MISCELLANEOUS PENAL LAW', 'DANGEROUS WEAPONS']
sr = pd.Series(data=counts, index=categories)

# | using pandas/matplotlib barplot
plt.figure()
sr.plot.barh()

# | using seaborn barplot
plt.figure()
sns.barplot(x=sr.values, y=sr.index)

Output