简体   繁体   English

如何创建带有辅助 y 轴的 matplotlib 图?

[英]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.我想使用 matplotlib 创建一个折线图,其中COMPMKR在一个轴上, LENDKNC在第二个 y 轴上。 The X-axis should be the date column. X 轴应该是日期列。 I only want dates from 2020-07-18 to 2020-07-20 .我只想要从2020-07-182020-07-20的日期。 The below output is a pandas dataframe.下面的 output 是 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

You can use loc to extract data in the date range, and plot :您可以使用loc提取日期范围内的数据,以及plot

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

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM