Har*_*hid 4 python matplotlib pandas
我想并排绘制两个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)
Run Code Online (Sandbox Code Playgroud)
没有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)
Run Code Online (Sandbox Code Playgroud)
完整代码:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2243 次 |
| 最近记录: |