john doe
john doe

Reputation: 2253

How to plot a two column pandas dataframe's elements as an histogram?

I have the following pandas dataframe:

    A                    B

    1                    3
    0                    2
    1                    4
    0                    1
    0                    3

I would like to plot the frequency of B instnaces given A, something like this:

     |
     |
     |        __
 B   |       |  |
     |  ___  |  |
     |  | |  |  |
     |  | |  |  |
     |__|_|__|__|______________
                A

Thus, I tried the following:

df2.groupby([df.A, df.B]).count().plot(kind="bar")

However, I am getting the following exception:

TypeError: Empty 'DataFrame': no numeric data to plot

Therefore, my question is how to plot the frequency of the elements in B given the frequency of A?.

Upvotes: 3

Views: 8874

Answers (4)

alphiii
alphiii

Reputation: 1645

Here is my way:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([[1,3],[0,2],[1,4],[0,1],[0,3]])
df.columns = ['A', 'B']
x = df.loc[:,'A'].values
y = df.loc[:,'B'].values
plt.bar(x, y, label = 'Bar', align='center',)
plt.xticks(x)
plt.show()

enter image description here

Upvotes: 2

Joe T. Boka
Joe T. Boka

Reputation: 6583

Sounds like this is what you want: You can use Series.value_counts()

print(df['B'].value_counts().plot(kind='bar'))

enter image description here

If you don't want the value_count sorted, you can do this:

print(df['B'].value_counts(sort=False).plot(kind='bar'))

enter image description here

Upvotes: 3

3kt
3kt

Reputation: 2553

I'm not entirely sure what you mean by "plot the frequency of the elements in B given the frequency of A", but this gives the expected output :

In [4]: df
Out[4]: 
      A  B
3995  1  3
3996  0  2
3997  1  4
3998  0  1
3999  0  3

In [8]: df['data'] = df['A']*df['B']

In [9]: df
Out[9]: 
      A  B  data
3995  1  3     3
3996  0  2     0
3997  1  4     4
3998  0  1     0
3999  0  3     0

In [10]: df[['A','data']].plot(kind='bar', x='A', y='data')
Out[10]: <matplotlib.axes._subplots.AxesSubplot at 0x7fde7eebb9e8>

In [11]: plt.show()

enter image description here

Upvotes: 2

Sreejith Menon
Sreejith Menon

Reputation: 1087

I believe if you are trying to plot the frequency of occurrence of values in column b, this might help.

from collections import Counter
vals = list(df['b'])
cntr = Counter(vals)
# Out[30]: Counter({1: 1, 2: 1, 3: 2, 4: 1})

vals = [(key,cntr[key]) for key in cntr]
x = [tup[0] for tup in vals]
y = [tup[1] for tup in vals]

plt.bar(x,y,label='Bar1',color='red')
plt.show()

Plot

Another way using histogram from matplotlib. First declare a bins array, which are basically buckets into which your values will go into.

import matplotlib.pyplot as plt
import pandas as pd

l = [(1,3),(0,2),(1,4),(0,1),(0,3)]
df = pd.DataFrame(l)

df.columns = ['a','b']
bins = [1,2,3,4,5] #ranges of data
plt.hist(list(df['b']),bins,histtype='bar',rwidth=0.8)

Upvotes: 1

Related Questions