简体   繁体   中英

Histogram Plotting Python

I need histograms on the number of attributes and classes, description of attribute and classes and the number of instances and classes, while being new to program this is what I've tried so far.

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

data=pd.read_csv('mushroom')
column=df.'Class'
num_bins = 5
n, bins, patches = plt.hist(column, num_bins, facecolor='blue', alpha=0.5)
plt.show()

This is how my data looks like

cap-shape,cap-surface,cap-color,bruises,odor,gill-attachment,gill-spacing,gill-size,gill-color,stalk-shape,stalk-surface-above-ring,stalk-surface-below-ring,stalk-color-above-ring,stalk-color-below-ring,veil-type,veil-color,ring-number,ring-type,spore-print-color,population,habitat,Class
,f,g,f,n,f,c,n,p,e,s,s,w,w,p,w,o,p,k,v,u,p
,f,g,f,n,f,c,n,n,e,s,s,w,w,p,w,o,p,k,y,u,p
x,f,g,f,n,f,w,b,k,t,s,f,w,w,p,w,o,e,n,s,g,e
,f,g,f,n,f,c,n,g,e,s,s,w,w,p,w,o,p,n,y,u,e
x,f,w,f,n,f,w,b,p,t,f,s,w,w,p,w,o,e,n,a,g,e
s,f,n,f,n,f,c,n,n,e,s,s,w,w,p,w,o,p,k,v,u,e
f,f,n,f,n,f,c,n,n,e,s,s,w,w,p,w,o,p,n,v,u,e
x,f,g,f,n,f,c,n,p,e,s,s,w,w,p,w,o,p,n,y,u,e
f,s,g,f,n,f,w,b,n,t,s,f,w,w,p,w,o,e,n,s,g,e
x,f,w,f,n,f,w,b,n,t,f,f,w,w,p,w,o,e,n,a,g,e
x,s,n,f,n,f,w,b,p,t,f,f,w,w,p,w,o,e,k,s,g,e
x,s,w,f,n,f,w,b,h,t,f,s,w,w,p,w,o,e,n,s,g,p
f,f,w,f,n,f,w,b,p,t,f,s,w,w,p,w,o,e,k,s,g,p
x,f,g,f,n,f,w,b,p,t,f,f,w,w,p,w,o,e,n,s,g,e

Class is a categorical variable (or a factor http://www.statisticshowto.com/what-is-a-categorical-variable/ ). Binning and histogram is meaningful when you have a continuous variable ( http://www.statisticshowto.com/continuous-variable/ ).

I assume what you actually need is a frequency plot, a bar chart that shows the frequency of each outcome in your categorical data.

If my assumption is correct, the following code will solve your problem.

import pandas
import matplotlib.pyplot as plt

data = pandas.read_csv('mushroom.csv')
fig, ax = plt.subplots()
data['Class'].value_counts().plot(kind='bar', ax=ax)  
plt.show()

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