Subplot辅助轴 - Python,matplotlib

时间:2016-01-13 02:01:28

标签: python matplotlib plot seaborn

我有一个名为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

我打算在每个州绘制主要Y轴的应用数量和贷款数量以及辅助轴的转换比率。

我尝试了以下代码:

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)

我最终得到如下图:

enter image description here

问题是如何显示辅助轴线?如果我尝试以下(根据plot()中的手动设置secondary_y应选择性地绘制辅助轴中的那些列),我只看到在辅助轴上绘制的线I。我必须要有一些简单而明显的东西。我无法弄清楚它是什么!任何大师都可以帮忙吗?

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

1 个答案:

答案 0 :(得分:1)

您需要在conversionration以及y=['Loans', 'Apps','conversionratio']中添加secondary_y ...或者更好地保留该参数,因为您正在绘制所有列。< / p>

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)

enter image description here