5

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:

enter image description here

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
hernanavella
  • 5,462
  • 8
  • 47
  • 84
  • @importanceOfBeingErnest If you take the time to understand the problem instead of flagging immediately, you notice that my issue is joining at the index, not at the values. – hernanavella Mar 16 '18 at 19:13
  • @ImportanceOfBeingErnest the chart was just to indicate my desired output. What do I need to do so you unblock the question so I can get help? – hernanavella Mar 16 '18 at 19:57
  • @ImportanceOfBeingErnest You are being unreasonable. the code of a simple line is clear plot(df1), however Im showing I don't know how to apply the make-every argument with an index, and that is not shown in the duplicate – hernanavella Mar 16 '18 at 20:04
  • @ImportanceOfBeingErnest You know its plt.plot(df1['b']), that's not the problem. I will edit the question. But let's not distract from the main problem. How do you use markevery for index? – hernanavella Mar 16 '18 at 20:09
  • Markevery is always used for the index. Even in the duplicate. So I'm not sure where exactly your problem was, but since the question is now at least according to the rules, it's fair to reopen. – ImportanceOfBeingErnest Mar 16 '18 at 20:23

1 Answers1

2

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712