当我有两个单独的DataFrame并尝试使用子图将它们绘制在单独的图形中时,它不起作用(我知道,我应该存储由子图创建的轴并将其提供给DataFrame):
df1 = pd.DataFrame([2, 4, 8], columns=['data1'])
df2 = pd.DataFrame([3.2, 3.4, 3.3], columns=['data2'])
plt.subplot(1, 2, 1)
df1.plot() # ignores the created subplot
plt.subplot(1, 2, 2)
df2.plot() # ignores the created subplot
但是奇怪的是,如果我将数据收集到一个DataFrame中,那么这种语法就可以使用
df1['data2'] = df2
plt.subplot(1, 2, 1)
df1['data1'].plot() # now subplot is recognized and used!
plt.subplot(1, 2, 2)
df1['data2'].plot() # now subplot is recognized and used!
有人知道为什么第二部分使用创建的子图,而第一部分却不使用吗?只是巧合?还是有原因?