Multiindex scatter plot

∥☆過路亽.° 提交于 2019-12-12 18:56:52

问题


Suppose I have the following data:

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?


回答1:


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)

Output:



来源:https://stackoverflow.com/questions/46041166/multiindex-scatter-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!