imantha
imantha

Reputation: 3838

Plotting subplots using dataframes

I have dictionary containing 9 dataframes. I want to create a 3,3 subplot and plot bar charts for each dataframe.

To plot a single plot I would do this (just a singplot not considering subplots),

%matplotlib inline
with plt.style.context('bmh'):
    famd = satFAMD_obj['MODIS.NDVI']
    df_norm = satFAMD_dfNorm['MODIS.NDVI']
    df_cor =famd.column_correlations(df_norm)
    df_cor.columns = ['component 1','component 2', 'component 3']
    df_cor.plot(kind = 'bar',cmap = 'Set1', figsize = (10,6))
plt.show()

where satFAMD_obj & satFAMD_dfNorm are two dictionaries containing factor analysis trained objects and a dataframes. In the next line I create a new dataframe called df_cor and then plot it using this line df_cor.plot(kind = 'bar',cmap = 'Set1', figsize = (10,6)).

Now my problem is when it comes to multiple subplots how do I do this ? I cannot simply do this,

fig,ax = plt.subplots(3,3, figsize = (12,8))
ax[0,0].df_cor.plot(kind = 'bar',cmap = 'Set1')

Any ideas?

Upvotes: 1

Views: 47

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169524

I'm supposing that all of your keys in your two dictionaries will need to be plotted.

You will:

  • declare subplots,
  • iterate over dictionaries,
  • iterate over the axes objects,
  • plot to each set of axes.

Using code like the below example:

fig,ax = plt.subplots(3,3, figsize = (12,8))

for k1,k2 in zip(satFAMD_obj.keys(),satFAMD_dfNorm.keys()):
    for axes in ax.flatten():
        famd = satFAMD_obj[k1]
        df_norm = satFAMD_dfNorm[k2]
        df_cor = famd.column_correlations(df_norm)
        df_cor.columns = ['component 1','component 2', 'component 3']
        df_cor.plot(kind = 'bar',cmap = 'Set1',ax=axes)
        #                                      ^^^^^^^

Upvotes: 1

Related Questions