使用 Seaborn 或 Matplotlib 基于两个不同 pandas 数据帧(具有相同结构)的记录的子图

subplots based on records of two different pandas DataFrames ( with same structure) using Seaborn or Matplotlib

我有两个 DataFrame,如下所示。两者具有相同的结构(列名和索引)但值不同。 DataFrame 1 是观测值,DataFrame 2 是预测值。我想使用子图绘制一个图形,每个子图代表一列,Y 轴是来自两个数据帧(两条不同的线)的值,X 轴是索引。

我知道我应该 post 我工作的示例代码,但似乎它们都是错误的,这就是为什么我不分享我的示例代码。我真的很想使用像 seaborn 的 sns.FacetGrid 这样的东西,因为这些图质量更高。

    08FB006 08FC001 08FC003 08FC005 08GD004
----------------------------------------------
0   253      872    256      11.80    2660
1   250      850    255      10.60    2510
2   246      850    241      10.30    2130
3   241      827    235      9.32     1970
4   241      821    229      9.17     1900
5   232      819    228      8.93     1840
6   231      818    225      8.05     1710
7   234      817    225      7.90     1610
8   210      817    224      7.60     1590
9   200      816    221      7.53     1590
10  199      810    219      7.41     1550

您可以根据需要修改以下代码-

actual = pd.DataFrame({'a': [5, 8, 9, 6, 7, 2],
                       'b': [89, 22, 44, 6, 44, 1]})
predicted = pd.DataFrame({'a': [7, 2, 13, 18, 20, 2],
                       'b': [9, 20, 4, 16, 40, 11]})

# Creating a tidy-dataframe to input under seaborn
merged = pd.concat([pd.melt(actual), pd.melt(predicted)]).reset_index()
merged['category'] = ''
merged.loc[:len(actual)*2,'category'] = 'actual'
merged.loc[len(actual)*2:,'category'] = 'predicted'

g = sns.FacetGrid(merged, col="category", hue="variable")
g.map(plt.plot, "index", "value", alpha=.7)
g.add_legend();