Jonathan
Jonathan

Reputation: 33

How to create a matplotlib graph with a secondary y-axis?

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

Upvotes: 0

Views: 113

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

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

Upvotes: 1

Related Questions