简体   繁体   中英

How to plot overlapping series using line and markers?

Given the following data:

df1

               a       b    c
 1/1/2017   -162    1525     -41
 1/2/2017    192    1530      86
 1/3/2017     33    1520    -124
 1/4/2017    173    1502    -108
 1/5/2017    194    1495     -31
 1/6/2017    -15    1520     -46
 1/7/2017     52    1525     181
 1/8/2017     -2    1530    -135
 1/9/2017     37    1540      65
1/10/2017    197    1530      73

df2

              a
1/3/2017     33
1/6/2017    -15
1/7/2017     52
1/8/2017     -2
1/9/2017     37

How can I produce at chart using matplotlib, that plots 'b' column of df1 and on top of that, puts markers on same plot line, but using the index points from df2 .

The desired chart would look something like this:

在此输入图像描述

I looked at this answer but can't quite adapt it. The issue is that in the example they use the values, but in my case the part that is common between the two data-sets is the index

This is code from the referenced question that I tried:

xs = df1['b']
ys = df2['a'] # ---> this doesn't make sense here....
markers_on = df2.index
plt.plot(xs, ys, '-gD', markevery=markers_on)
plt.show()

But the chart comes back empty:

TypeError: <class 'NoneType'> type object None

I also tried

xs = df1['b']
markers_on = list(df2.index)
plt.plot(xs, '-gD', markevery=markers_on)
plt.show()

But I get

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing

The there are different possible formats for markevery. None of them uses the actual values to mark. Here, it makes sense to either use the indices of the values to mark, or a boolean array of the same length as the data. The latter would look like this:

import numpy as np 

markers_on = np.isin(df1.index, df2.index) 

plt.plot(df1["b"], '-gD', markevery=list(markers_on))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM