Home > database >  Plotting subplot inside subplot
Plotting subplot inside subplot

Time:08-25

I am trying to plot a subplot inside a subplot:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from pandas import util
import pandas.util.testing as testing
import matplotlib.pyplot as plt
import pandas as pd

df = util.testing.makeDataFrame()


with mpl.rc_context(rc={'font.family': 'serif', 'font.weight': 'light', 'font.size': 12}):
    fig = plt.figure(figsize= (12, 6), dpi =200)

    ax = fig.add_subplot(2, 2, (1,2))
    ax2 = ax.twinx()
    df['A'].plot(ax=ax, color = 'g')
    df['B'].plot(ax=ax2, color ='g')

    fig.add_subplot(223)
    df['C'].plot(color='r')
    plt.axvline(x = 7, color = 'b', label = 'axvline - full height')
    
    fig.add_subplot(2,2,4)
    df = df.round(0)
    ax3 = df['D'].plot(kind='bar')
    ax3.legend( prop={'size': 6})
    
    fig.tight_layout()
    plt.show()

Will produce this: enter image description here

I am trying to get access to fig.add_subplot(223) so I can insert this code, which will allow me to plot another subplot inside the subplot.

fig = plt.figure(dpi =200)
fig.subplots_adjust(hspace= 0)
ax5 = fig.add_subplot(3,1,(1,2))
df['A'].plot(color='r')
plt.axvline(x = 7, color = 'b', label = 'axvline - full height')

ax6 = fig.add_subplot(3,1,(3) , sharex = ax5)
df['A'].plot(color='r')

So that this can go instead of figure (223) enter image description here

Could you please advise what I am missing in the hierarchy? As in the main fig has subplots, how can I declare another fig inside the subplot?

CodePudding user response:

You can use a enter image description here

  • Related