簡體   English   中英

將兩個pandas數據框並排繪制,每個都以子圖樣式繪制

[英]Plot two pandas data frames side by side, each in subplot style

我想並排繪制兩個pandas數據幀,每個繪圖應該是子圖形式。 我使用以下行:

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)

沒有subplot選項,繪制了並排圖,但我想要在subplot樣式。 有沒有其他選擇來完成它?

確切地說,我想以下列方式繪制大熊貓: 在此輸入圖像描述

您需要一個包含3行和2列的子繪圖網格來托管您的繪圖。 然后ax參數需要采用您想要繪制3個數據幀列的3個軸。

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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM