How to build overlapping rows using lines and markers?

Given the following data:

df1 abc 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 create on a chart using matplotlib, which displays the column 'b' df1 and, in addition, puts the markers on the same storyline, but using index points from df2 .

The desired chart will look something like this:

enter image description here

I looked at this answer , but could not fully adapt it. The problem is that in this example they use values, but in my case the part that is common between the two datasets is the index

This is the code from the question 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 returns 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 
+5
source share
1 answer

There are various possible formats for markevery. None of them use actual values ​​to mark. It makes sense to either use indexes of values ​​for the mark, or a logical array of the same length as the data. The latter will 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() 
+2
source

Source: https://habr.com/ru/post/1275926/


All Articles