Plot histogram in python using csv file as input

I have a csv file that contains two columns, where the first column is the name of the fruit and the second column is count, and I need to plot a bar chart using this csv like the input code below. How to do it. I just have to show the first 20 records where the fruit names will be the x axis and count will be the y axis from the whole 100 line csv file.

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', header = None ,quoting=2)

data.hist(bins=10)
plt.xlim([0,100])
plt.ylim([50,500])
plt.title("Data")
plt.xlabel("fruits")
plt.ylabel("Frequency")
plt.show()

      

I have edited the above program for plotting histogram -

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', sep=',',header=None)
data.values
print data
plt.bar(data[:,0], data[:,1], color='g')
plt.ylabel('Frequency')
plt.xlabel('Words')
plt.title('Title')

plt.show()

      

but it gives me the "Unhashable Type" error. Can anyone help on this.

+3


source to share


1 answer


You can use an inline graph pandas

, although you need to specify the first column - the index,

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('data.csv', sep=',',header=None, index_col =0)

data.plot(kind='bar')
plt.ylabel('Frequency')
plt.xlabel('Words')
plt.title('Title')

plt.show()

      



If you need to use matplotlib

it might be easier to convert the array to dictionary with data.to_dict()

and extract the data to a numpy array or something.

+3


source







All Articles