我有以下两个df是指数和泊松图

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()

最佳答案

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

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

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

关于python - matplotlib中的并排图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50016961/

10-12 21:18