多索引散点图

时间:2017-09-04 16:33:44

标签: python pandas matplotlib scatter

假设我有以下数据:

data = {'Value': {('1', 1): 3.0,
('1', 2): 4.0,
('1', 3): 51.0,
('1', 4): 10.0,
('1', 5): 2.0,
('1', 6): 17.0,
('1', 7): 14.0,
('1', 8): 7.0,
('1', 9): 2.0,
('1', 10): 1.0}}
df=pd.DataFrame(data)

让我们说这代表了1月份前十天的价值。我想绘制这些数据,所以我使用:

df.plot()
plt.show()

现在,假设我有另一个数据集,其中包含这些日期的子集的值,但值略有不同但索引值相同:

df1 = df[df['Value']<10]
df1['Value'] = df1['Value']*2

我的问题是,如何在原始折线图上叠加此数据的散点图?

1 个答案:

答案 0 :(得分:3)

抓住第一个图的轴手柄,然后重新索引df1,使用与df相同的索引对齐数据,并使用ax=ax绘制df1。

ax = df.plot()
df1.reindex(df.index).plot(marker='o',linestyle='none',color='g', ax=ax)

输出:

enter image description here