Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn or matplotlib bar plot multiple dataframes side by side

Using matplotlib I was trying to put bar plots next to each other. This is pretty common and I've gone through a bunch of stackoverflow pages but something still isn't right.

df1

    Net Count   date
0   AA  242624806   2018-03-01 00:00:00.000
1   AA  213729127   2018-03-01 00:01:00.000
2   AA  4482234727  2018-03-01 00:02:00.000
3   AA  26042386    2018-03-01 00:03:00.000
4   AA  13444400    2018-03-01 00:04:00.000

df2

    Net Count   date
0   BB  242806      2018-03-01 00:00:00.000
1   BB  729127      2018-03-01 00:01:00.000
2   BB  85872722    2018-03-01 00:02:00.000
3   BB  26006231    2018-03-01 00:03:00.000
4   BB  123115400   2018-03-01 00:04:00.000

df3

    Net Count   date
0   CC  452806      2018-03-01 00:00:00.000
1   CC  129127      2018-03-01 00:01:00.000
2   CC  858722      2018-03-01 00:02:00.000
3   CC  26216231    2018-03-01 00:03:00.000
4   CC  33115400    2018-03-01 00:04:00.000

Code:

x=df['date']  #since the date are the same in both tables I only have 1 x
y=df['count']
y2=d2['count']
y3=d2['count']

plt.figure(figsize=(15,8))
plt.bar(x,y,label="AA")
plt.bar(x,y2,label="BB")
plt.bar(x,y3,label="CC")

plt.title("Count by Networks")
plt.legend(title="Network")
plt.show()

Here is how it looks : enter image description here But I've tried align=edge, align=center and playing around with the widths but it is always overlapping.

How would I make this work so that the bars are not stacked so they are side by side?

Like this: enter image description here


**** Updated with Answer *****
Y.Luo this worked for me the best in pandas like this:

dateindex=df1['date']

aa=dict(zip(x,df1['count']))
bb=dict(zip(x,df2['count']))
cc=dict(zip(x,df3['count']))
dd=dict(zip(x,df4['count']))
ee=dict(zip(x,df5['count']))


dfbar = pd.DataFrame({'AA': aa, 'BB': bb, 'CC': cc,'DD': dd, 'EE': ee}, index=dateindex)

# Non-stacked bar plot
dfbar.plot.bar(figsize=(16, 6))

plt.title("Count by Networks")
plt.legend(title="Network")
plt.show() 
like image 561
chowpay Avatar asked Apr 17 '26 07:04

chowpay


1 Answers

If you want a non-stacked bar plot with matplotlib, you would need to adjust the position for each dataframe yourself like this:

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

# Example data
n=24
dateindex = pd.date_range(pd.datetime(2018, 1, 1), periods=n)
np.random.seed(1)
aa = pd.DataFrame(np.random.randn(n), columns=['count'], index=dateindex)
np.random.seed(2)
bb = pd.DataFrame(np.random.randn(n), columns=['count'], index=dateindex)
np.random.seed(3)
cc = pd.DataFrame(np.random.randn(n), columns=['count'], index=dateindex)

# Non-stacked bar plot
plt.figure(figsize=(16, 6))
width = 0.25
plt.bar(np.arange(len(aa))-width, aa.values, width, label="AA")
plt.bar(np.arange(len(aa)), bb.values, width, label="BB")
plt.bar(np.arange(len(aa))+width, cc.values, width, label="CC")
plt.xticks(np.arange(len(aa)), dateindex, rotation='vertical')

plt.title("Count by Networks")
plt.legend(title="Network")
plt.show()

Non-stacked bar plot with matplotlib

ImportanceOfBeingErnest is correct. Pandas is the easiest since it does the adjustment for you:

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

# Example data
n=24
dateindex = pd.date_range(pd.datetime(2018, 1, 1), periods=n)
np.random.seed(1)
aa = np.random.randn(n)
np.random.seed(2)
bb = np.random.randn(n)
np.random.seed(3)
cc = np.random.randn(n)
df = pd.DataFrame({'AA': aa, 'BB': bb, 'CC': cc}, index=dateindex)

# Non-stacked bar plot
df.plot.bar(figsize=(16, 6))

plt.title("Count by Networks")
plt.legend(title="Network")
plt.show()
like image 137
Y. Luo Avatar answered Apr 20 '26 01:04

Y. Luo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!