简体   繁体   English

子图辅助轴-Python,matplotlib

[英]Subplot secondary axis - Python, matplotlib

I have a dataframe called conversionRate like this: 我有一个名为conversionRate的数据框,如下所示:

| State| Apps  | Loans| conversionratio|
2013-01-01  IL      1165    152   13.047210
2013-01-01  NJ      2210    756   34.208145
2013-01-01  TX      1454    73    5.020633
2013-02-01  CA      2265    400   17.660044
2013-02-01  IL      1073    168   15.657036
2013-02-01  NJ      2036    739   36.296660
2013-02-01  TX      1370    63    4.598540
2013-03-01  CA      2545    548   21.532417
2013-03-01  IL      1108    172   15.523466

I intend to plot the number of apps and number of loans in the primary Y axis and the Conversion Ratio in the secondary axis for each state. 我打算在每种州的主要Y轴上绘制应用程序数量和贷款数量,并在次要轴上绘制转化率。

I tried the below code: 我尝试了以下代码:

import math
rows =int(math.ceil(len(pd.Series.unique(conversionRate['State']))/2))
    fig, axes = plt.subplots(nrows=rows, ncols=2, figsize=(10, 10),sharex=True, sharey=False)
    columnCounter = itertools.cycle([0,1])
    rowCounter1    = 0
    for element in pd.Series.unique(conversionRate['State']):
        rowCounter  = (rowCounter1)//2
        rowCounter1 = (rowCounter1+1)
        subSample = conversionRate[conversionRate['State']==element]
        axis=axes[rowCounter,next(columnCounter)]
        #ax2 = axis.twinx()
        subSample.plot(y=['Loans', 'Apps'],secondary_y=['conversionratio'],\
                              ax=axis)

I end up with a figure like the below: 我最终得到如下图:

在此处输入图片说明

The question is how do I get the secondary axis line to show? 问题是如何显示副轴线? If I try the below (per the manual setting secondary_y in plot() should selectively plot those columns in the secondary axis), I see only the line I plot on the secondary axis. 如果我尝试以下操作(按照plot()中的手动设置secondary_y应该有选择地在辅助轴上绘制那些列),我只会看到我在辅助轴上绘制的线。 There must be something simple and obvious I am missing. 我肯定缺少一些简单明了的东西。 I can't figure out what it is! 我不知道这是什么! Can any guru please help? 上师可以帮忙吗?

subSample.plot(secondary_y=['conversionratio'],ax=axis)

You need to include conversionration in y=['Loans', 'Apps','conversionratio'] as well as in secondary_y ... or better yet leave that parameter out, since you're plotting all the columns. 您需要在y=['Loans', 'Apps','conversionratio']以及secondary_y ...中包含conversionration ,或者最好将其排除在外,因为您正在绘制所有列。

rows =int(math.ceil(len(pd.Series.unique(conversionRate['State']))/2))
fig, axes = plt.subplots(nrows=rows, ncols=2, figsize=(10, 
10),sharex=True, sharey=False)
columnCounter = itertools.cycle([0,1])
rowCounter1    = 0
for element in pd.Series.unique(conversionRate['State']):
    rowCounter  = (rowCounter1)//2
    rowCounter1 = (rowCounter1+1)
    subSample = conversionRate[conversionRate['State']==element]
    axis=axes[rowCounter,next(columnCounter)]
    #ax2 = axis.twinx()
    subSample.plot(secondary_y=['conversionratio'], ax=axis)

在此处输入图片说明

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

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