3

I have a csv file which contains two columns where first column is fruit name and second column is count and I need to plot histogram using this csv as input to the code below. How do I make it possible. I just have to show first 20 entries where fruit names will be x axis and count will be y axis from entire csv file of 100 lines.

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 edited the above program to plot a bar chart -

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 this gives me an error 'Unhashable Type '. Can anyone help on this.

6
  • can you put an example of your csv file? Commented Jun 7, 2015 at 8:55
  • 3
    you want to plot barchart not histograms. You can't put categorical data as x axis on histogram. Commented Jun 7, 2015 at 8:59
  • You never actually pass your data to the plot. See the examples here: bespokeblog.wordpress.com/2011/07/11/… and here: people.duke.edu/~ccc14/pcfb/numpympl/MatplotlibBarPlots.html
    – abalter
    Commented Jun 7, 2015 at 9:00
  • Sarit makes a good point. Are you looking at counts for different types of fruit? That is a barchart. A histogram is a plot of counts against a set of enumerable values, such as number of fruit eaten per day by your sample population--so many people eat 1 piece, so many 2 pieces, so many 3 pieces, etc.
    – abalter
    Commented Jun 7, 2015 at 9:03
  • yes a bar chart actually
    – Nick
    Commented Jun 7, 2015 at 9:06

1 Answer 1

6

You can use the inbuilt plot of pandas, although you need to specify the first column is 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 may be easier to convert the array to a dictionary using data.to_dict() and extract the data to numpy array or something.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.