Reputation: 5552
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
Upvotes: 5
Views: 734
Reputation: 339350
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()
Upvotes: 2