matplotlib中的并排图

时间:2018-04-25 08:00:09

标签: python pandas matplotlib

我有以下2 df s指数和泊松情节

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

mean = 2
step = 0.5
df1 = pd.DataFrame()
df1['A'] = pd.Series(abs(np.random.exponential(step, 400)))
df2 = pd.DataFrame()
df2['B'] = pd.Series(abs(np.random.poisson(mean, 400)))

df1_summ = df1_summary[['A']].groupby(['A']).size().reset_index(name='counts')
df1_summ = df1_summ.sort_values(['counts'], ascending=True)
df2_summ = df2[['B']].groupby(['B']).size().reset_index(name='counts')

在Jupyter笔记本中,我希望将它们并排绘制。但是,以下代码不允许它。我该如何解决?

plt.figure(1)
plt.subplot(221)
df1_summ.plot.bar(x='A', y='counts', figsize=(5, 4), title='Exponential Plot')

plt.figure(2)
plt.subplot(222)
df2_summ.plot.bar(x='B', y='counts', figsize=(5, 4), title='Poisson Plot')

plt.show()

2 个答案:

答案 0 :(得分:3)

您只需要创建一个图形,然后创建子图并将它们作为参数传递给绘图函数:

fbLoginManager.loginBehavior = FBSDKLoginBehavior.web

答案 1 :(得分:0)

作为替代方案。

fig = plt.figure()
fig.set_figheight(20) # optional setting the height of the image
fig.set_figwidth(20) # optional setting the width of the image

a = fig.add_subplot(1,2,1)
df1_summ.plot.bar(x='A', y='counts')
a.set_title=('Exponential Plot')
plt.axis('off') # removes plots on axis

a = fig.add_subplot(1,2,2)
df2_summ.plot.bar(x='B', y='counts')
a.set_title=('Poisson Plot')
plt.axis('off') #romoves plots on axis