0

I would like to create a line graph using matplotlib, where COMP and MKR are on one axis, and LEND, KNC are on a secondary y-axis. The X-axis should be the date column. I only want dates from 2020-07-18 to 2020-07-20. The below output is a pandas dataframe.

How can I do that? Thanks!!


              COMP   LINK    LEND    KNC     MKR
date                                            
2020-07-16  154.84  8.350  0.2766  1.571  443.10
2020-07-17  167.20  8.261  0.2883  1.636  449.49
2020-07-18  163.72  7.971  0.3110  1.826  454.50
2020-07-19  163.40  8.083  0.3534  1.689  465.80
2020-07-20  166.00  7.932  0.3519  1.710  461.10
Jonathan
  • 33
  • 4
  • You can find the solution here: https://stackoverflow.com/questions/14762181/adding-a-y-axis-label-to-secondary-y-axis-in-matplotlib – merenptah Jul 20 '20 at 12:37

1 Answers1

1

You can use loc to extract data in the date range, and plot:

df.loc['2020-07-18':'2020-07-20'].plot(y=['COMP', 'MKR','LEND','KNC'],secondary_y=['LEND','KNC'])

Output:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Thanks for the answer. Is there a way I can customize the y axis ranges for both the original and secondary axis? (i.e. say I wanted to make seconday axis vary from 0 - 1.2) – Jonathan Jul 20 '20 at 12:54