0

I have the following code for 27 subplots in a 9x3 single plot. I want to add specific individual titles to each subplot, eg. "District 7 - Brooklyn." However, this code repeats both the data and the title for all 27 subplots, presumably because of the last line of code. I have consulted this previous SO question and tried all three of those methods, but each one erases the data in the subplot when the title is added or is broadcast incorrectly.

 fig, axes = plt.subplots(9,3,figsize=(30,80))

for ax in axes.flatten():
    ax1 = district_1_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax1.set_title("District 1: Eastern Long Island", fontsize=18) 
    ax2 = district_2_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax2.set_title("District 2: Southern Long Island", fontsize=18) 
    ax3 = district_3_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax3.set_title("District 3: Northern Long Island", fontsize=18) 
    ax4 = district_4_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax4.set_title("District 4: Central, Southern Nassau County", fontsize=18) 
    ax5 = district_5_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax5.set_title("District 5: Queens", fontsize=18) 
    ax6 = district_6_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax6.set_title("District 6: Queens", fontsize=18) 
    ax7 = district_7_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax7.set_title("District 7: Queens", fontsize=18) 
    ax8 = district_8_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax8.set_title("District 8: Brooklyn, Queens", fontsize=18) 
    ax9 = district_9_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax9.set_title("District 9: Brooklyn", fontsize=18) 
    ax10 = district_10_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax10.set_title("District 10: Upper West Side, FiDi, Greenwich Village, Borough Park (BK)", fontsize=18) 
    ax11 = district_11_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax11.set_title("District 11: Staten Island, Southern Brooklyn", fontsize=18) 
    ax12 = district_12_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax12.set_title("District 12: Eastern Manhattan, Greenpoint (BK), Western Queens", fontsize=18) 
    ax13 = district_13_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax13.set_title("District 13: Upper Manhattan, Bronx", fontsize=18) 
    ax14 = district_14_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax14.set_title("District 14: Eastern Bronx, North Central Queens", fontsize=18) 
    ax15 = district_15_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax15.set_title("District 15: Southern, Western Bronx", fontsize=18) 
    ax16 = district_16_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax16.set_title("District 16: North Bronx, Southern Westchester ", fontsize=18) 
    ax17 = district_17_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax17.set_title("District 17: Rockland, Westchester", fontsize=18) 
    ax18 = district_18_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax18.set_title("District 18: Orange, Putnam, Southern Dutchess, Northeastern Westchester", fontsize=18) 
    ax19 = district_19_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax19.set_title("District 19: Hudson Valley, Catskills", fontsize=18) 
    ax20 = district_20_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax20.set_title("District 20: Albany, Schenectady", fontsize=18) 
    ax21 = district_21_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax21.set_title("District 21: Clinton, Essex, Franklin, Fulton, Hamilton, Herkimer, Jefferson, Lewis, Saratoga, St. Lawrence, Warren, and Washington", fontsize=18) 
    ax22 = district_22_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax22.set_title("District 22: Central New York", fontsize=18) 
    ax23 = district_23_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax23.set_title("District 23: Western New York", fontsize=18) 
    ax24 = district_24_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax24.set_title("District 24: Cayuga, Onondaga, Wayne, Western Oswego", fontsize=18) 
    ax25 = district_25_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax25.set_title("District 25: Monroe", fontsize=18) 
    ax26 = district_26_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax26.set_title("District 26: Western New York: Erie, Niagara", fontsize=18) 
    ax27 = district_27_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax) 
    ax27.set_title("District 27: Western New York", fontsize=18) 


# plt.tight_layout()   
thedatasleuth
  • 539
  • 4
  • 22
  • I don't believe this is a duplicate question and I even referenced the question you said it was duplicative of in my original post. I tried all three of those methods and each one just set the title and erased my data in the subplot, as I explained in my post – thedatasleuth Sep 14 '18 at 15:00

1 Answers1

0

Here is a minimal working solution (excluding imports) using a sample DataFrame for you using a smaller grid of 2x3. You can simply extend it to your actual grid and real data. The important lines which are used for setting the title are highlighted by a comment

fig, axes = plt.subplots(2,3,figsize=(16,8))

df = pd.DataFrame({'count': {0: 37, 1: 23, 2: 10, 3: 16, 4: 45}}).reset_index()

for ax in axes.flatten():
    axi = df.plot.bar(x='index', y='count', legend=False, ax=ax) # Notice ax=ax and axi here
    axi.set_title("Title here", fontsize=18) # Set the title here
plt.tight_layout()    

Output

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thank you so much - this is exactly what I wanted. – thedatasleuth Sep 14 '18 at 00:32
  • Sorry, one more question: for some reason, this code is now repeating the last plot for all 27 subplots. I have edited my question above to show the code I'm implementing now, based on your original answer. – thedatasleuth Sep 14 '18 at 15:07
  • I understood the problem but currently I am going somewhere and I will answer you after a couple of hours – Sheldore Sep 14 '18 at 15:10
  • No rush - thank you so much. – thedatasleuth Sep 14 '18 at 15:11
  • Well ok try this in the meantime: Write ax = axes.flatten() before the for loop. Since you are writing plotting lines and titles explicitly for each of the subplots, you dont need the for loop. Remove the line for for loop and in every plot command use ax=ax[0] in the first plot, ax=ax[1] in the second plot and so on up to 26 or whatever is the total number of subfigures minus 1 – Sheldore Sep 14 '18 at 15:17
  • Let me know If it works – Sheldore Sep 14 '18 at 15:18
  • Got it - thank you so much! – thedatasleuth Sep 14 '18 at 16:51