[Python] 2 plots inside subplot2grid

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using suplot2grid to chart 6 subplots and each one of these subplots has 2 time series inside. Each of these subplots should show values in different Y (vertical) axis.
    My current output, which is close to my objective, but still wrong, is like this: [​IMG]

    Right now What I do is the following:

    fig2 = plt.figure(figsize=(14,11))

    # defining the axes

    ax1_a = plt.subplot2grid((12,2),(0,0), rowspan=3, colspan=1) # drift
    ax1_b = plt.subplot2grid((12,2),(4,0), rowspan=3, colspan=1, sharex=ax1_a) # zscore drift
    ax1_b = ax1_a.twinx()

    ax2_a = plt.subplot2grid((12,2),(4,0), rowspan=3, colspan=1, sharex=ax1_a)
    ax2_b = plt.subplot2grid((12,2),(8,0), rowspan=3, colspan=1, sharex=ax1_a)
    ax2_b = ax2_a.twinx()

    ax3_a = plt.subplot2grid((12,2),(8,0), rowspan=3, colspan=1, sharex=ax1_a)
    ax3_b = plt.subplot2grid((12,2),(11,0), rowspan=3, colspan=1, sharex=ax1_a)
    ax3_b = ax3_a.twinx()

    ax4_a = plt.subplot2grid((12,2),(0,1), rowspan=3, colspan=1)
    ax4_b = plt.subplot2grid((12,2),(4,1), rowspan=3, colspan=1, sharex=ax4_a)
    ax4_b = ax4_a.twinx()

    ax5_a = plt.subplot2grid((12,2),(4,1), rowspan=3, colspan=1, sharex=ax4_a)
    ax5_b = plt.subplot2grid((12,2),(8,1), rowspan=3, colspan=1, sharex=ax4_a)
    ax5_b = ax5_a.twinx()

    ax6_a = plt.subplot2grid((12,2),(8,1), rowspan=3, colspan=1, sharex=ax4_a)
    ax6_b = plt.subplot2grid((12,2),(11,1), rowspan=3, colspan=1, sharex=ax4_a)
    ax6_b = ax6_a.twinx()

    defining the plotting functions


    def plot_drift(axx,label):
    axx.plot(df.index[-lenght:], df[label][-lenght:], linestyle='-', lw=2,
    label=label,color='b',alpha=1.00)

    def plot_zscore(axx,label):
    axx.plot(df.index[-lenght:], df[label][-lenght:], linestyle='-', lw=2,
    label=label,color='g',alpha=1.00)

    def vertical_labels_drift(axx):
    axx.set_ylabel('Vols')
    def vertical_labels_Zscore(axx):
    axx.set_ylabel('Zscore')

    def stdev(axx):
    axx.axhline(2,linewidth=2,color='yellow') # standard deviations
    axx.axhline(-2,linewidth=2,color='yellow') # standard deviations
    axx.axhline(3,linewidth=2,color='red')
    axx.axhline(-3,linewidth=2,color='red')


    # plotting the drift
    for i,j in zip([ax1_a,ax2_a,ax3_a,ax4_a,ax5_a,ax6_a],cols_drift):
    plot_drift(i,j)
    vertical_labels_drift(i)

    # plotting the zscore
    for i,j in zip([ax1_b,ax2_b,ax3_b,ax4_b,ax5_b,ax6_b], cols_drift_Z):
    plot_zscore(i,j)
    stdev(i)
    vertical_labels_Zscore(i)


    So my question is, what is the right way of defining the new plot to go inside the subplots? Because if you put the same location for ax1_a and ax1_b, ax1_a gets overwritten by ax1_b and only ax1_b gets charted. In other words, if you do this:

    ax1_a = plt.subplot2grid((12,2),(0,0), rowspan=3, colspan=1) # drift
    ax1_b = plt.subplot2grid((12,2),(0,0), rowspan=3, colspan=1, sharex=ax1_a) # zscore drift
    ax1_b = ax1_a.twinx()


    ax1_a won't show.

    Also I don't understand why my charts are showing even with the wrong location inputted in the first code shown. It looks like even if you put a different location to ax1_b, ax2_b, etc. when doing .twinx() it kind of resets that chart to the chart that is referring when doing .twinx().

    Continue reading...

Compartilhe esta Página