seaborn或matplotlib条形图并排绘制多个数据框
我使用matplotlib试图将条形图并排放置.这是很常见的,我浏览了一堆stackoverflow页面,但是仍然有些不对劲.
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
代码:
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()
外观如下:
但是我试过align=edge
,align=center
并尝试使用宽度,但是宽度总是重叠的.
Here is how it looks :
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?
赞:
****更新为答案*****
Y.Luo在这样的大熊猫中为我工作得最好:
Like this:
**** 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()
如果要使用matplotlib进行非堆叠的条形图,则需要自己调整每个数据框的位置,如下所示:
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()
ImportanceOfBeingErnest是正确的.熊猫是最简单的,因为它可以为您进行调整:
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()