并排绘制两个 pandas 数据框,每个都以子图样式 [英] Plot two pandas data frames side by side, each in subplot style

查看:52
本文介绍了并排绘制两个 pandas 数据框,每个都以子图样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想并排绘制两个熊猫数据框,每个图应采用子图形式.我正在使用以下几行:

I want to plot two pandas dataframes side by side, each plot should be in subplot form. I am using following lines:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create dummy pandas dataframes
pd1 = pd.DataFrame({'a':np.random.random(22),'b':np.random.random(22),'c':np.random.random(22)})
pd2 = pd.DataFrame({'J':np.random.random(22),'K':np.random.random(22),'P':np.random.random(22)})
#create subplot figure with having two side by side plots
fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(12,6))
# plot first pandas frame in subplot style
pd1.plot(ax = axes[0],subplots=True) 
# plot second pandas frame in subplot style
pd2.plot(ax = axes[1],subplots=True)

没有子图选项,将绘制并排图,但是我想要子图样式.有没有其他选择来完成它?

Without subplot option, side by side plots are drawn but I want in subplot style. Is there any other option to get it done?

准确地说,我想通过以下方式绘制熊猫:

Precisely, I want to plot pandas in the following manner:

推荐答案

您需要一个 3 行 2 列的子图网格来承载您的图.然后, ax 参数需要采用您要将3个数据框列绘制到的3个轴.

You need a subplot grid of 3 rows and 2 columns to host your plot. Then the ax argument needs to take the 3 axes you want to plot the 3 dataframe columns to.

fig, axes = plt.subplots(nrows=3,ncols=2)
pd1.plot(ax = axes[:,0], subplots=True) 
pd2.plot(ax = axes[:,1], subplots=True)

完整代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create dummy pandas dataframes
pd1 = pd.DataFrame({'a':np.random.random(22),'b':np.random.random(22),
                    'c':np.random.random(22)})
pd2 = pd.DataFrame({'J':np.random.random(22),'K':np.random.random(22),
                    'P':np.random.random(22)})
#create subplot figure with having two side by side plots
fig, axes = plt.subplots(nrows=3,ncols=2,figsize=(12,6))
# plot first pandas frame in subplot style
pd1.plot(ax = axes[:,0],subplots=True) 
# plot second pandas frame in subplot style
pd2.plot(ax = axes[:,1],subplots=True)

plt.show()

这篇关于并排绘制两个 pandas 数据框,每个都以子图样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆