多索引散点图 [英] Multiindex scatter plot

查看:96
本文介绍了多索引散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

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)

比方说,这表示一月份前十天的值.我想绘制这些数据,所以我使用:

Let's say this represents values for something for the first ten days in January. I want to plot this data, so I use:

df.plot()
plt.show()

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

Now, suppose I have another data set that has values for a subset of these dates with slightly different values but the same index values:

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

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

My question is, how can I overlay a scatter plot of this data on the original line graph?

推荐答案

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

Grab the axes handle of the first plot, then reindex df1 to align the data with the same indexes as df and plot df1 using ax=ax.

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

输出:

这篇关于多索引散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆