Mr. Engineer
Mr. Engineer

Reputation: 375

Matplotlib how to count the occurence of specific value

I have a DataFrame like this:

          Apples       Oranges

0            1           1
1            2           1
2            3           2
3            2           3
4            1           2
5            2           3

I'm trying to count the occurence of values for both Apples and Oranges (how often values 1,2 and 3 occur in data frame for each fruit). I want to draw a bar chart using Matplotlib but so far I have not been successful. I have tried:

plt.bar(2,['Apples', 'Oranges'], data=df) 
plt.show()

But the output is very weird, could I have some advise? Thanks in advance.

Edit: I'm expecting result like this: enter image description here

Upvotes: 0

Views: 1124

Answers (4)

pakpe
pakpe

Reputation: 5479

Whenever you are plotting the count or frequency of something, you should look into a histogram:

from matplotlib import pyplot as plt
import pandas as pd

df = pd.DataFrame({'Apples': {0: 1, 1: 2, 2: 3, 3: 2, 4: 1, 5: 2}, 'Oranges': {0: 1, 1: 1, 2: 2, 3: 3, 4: 2, 5: 3}})
plt.hist([df.Apples,df.Oranges], bins=3,range=(0.5,3.5),label=['Apples', 'Oranges'])
plt.xticks([1,2,3])
plt.yticks([1,2,3])
plt.legend()
plt.show()

enter image description here

Upvotes: 0

Yilun Zhang
Yilun Zhang

Reputation: 9018

You can use the value_counts method together with pandas plotting:

# Sample data
d = {'apples': [1, 2,3,2,1,2], 'oranges': [1,1,2,3,2,3]}
df = pd.DataFrame(data=d)

# Calculate the frequencies of each value
df2 = pd.DataFrame({
    "apples": df["apples"].value_counts(),
    "oranges": df["oranges"].value_counts()
})

# Plot
df2.plot.bar()

You will get:

enter image description here

Upvotes: 3

sai
sai

Reputation: 1784

Here is another one

import pandas as pd

df = pd.DataFrame({'Apples': [1, 2, 3, 2, 1, 2], 'Oranges': [1, 1, 2, 3, 2, 3]})

df.apply(pd.Series.value_counts).plot.bar()

Upvotes: 1

FearoftheDark
FearoftheDark

Reputation: 26

You can use hist from matplotlib:

d = {'apples': [1, 2,3,2,1,2], 'oranges': [1,1,2,3,2,3]}
df = pd.DataFrame(data=d)

plt.hist([df.apples,df.oranges],label=['apples','oranges'])
plt.legend()
plt.show()

     

This will give the output:

histogram

Upvotes: 0

Related Questions