Search code examples
pythonhistogramfigurefig

Can someone help me rewrite this code (fig with multiple histograms) with a for loop or function?


I looked at a similar post but am still struggling with it. You don't have to write out the whole code if you don't want. But a syntactical guide would be helpful. Here is my lengthy manual approach:

# Grid of MMR distributions by every 5 years
mmr_1990 = mmr[mmr['Year']=='1990']
mmr_1995 = mmr[mmr['Year']=='1995']
mmr_2000 = mmr[mmr['Year']=='2000']
mmr_2005 = mmr[mmr['Year']=='2005']
mmr_2010 = mmr[mmr['Year']=='2010']
mmr_2015 = mmr[mmr['Year']=='2015']

year_list = [mmr_1990, mmr_1995, mmr_2000, mmr_2005, mmr_2010, mmr_2015]

fig = plt.figure(figsize=(12,8))

ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
ax4 = fig.add_subplot(2,3,4)
ax5 = fig.add_subplot(2,3,5)
ax6 = fig.add_subplot(2,3,6)

#1990
ax1.hist(mmr_1990['MMR'], color='darkred', edgecolor='white')
ax1.set_title('1990 MMR Distribution', fontweight='bold', loc='left')
ax1.spines['left'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
ax1.tick_params(length=0)
ax1.set_xlim(0,3000,500)

#1995
ax2.hist(mmr_1995['MMR'], color='darkred', edgecolor='white')
ax2.set_title('1995 MMR Distribution', fontweight='bold', loc='left')
ax2.spines['left'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.tick_params(length=0)
ax2.set_xlim(0,3000,500)

#2000
ax3.hist(mmr_2000['MMR'], color='darkred', edgecolor='white')
ax3.set_title('2000 MMR Distribution', fontweight='bold', loc='left')
ax3.spines['left'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.spines['right'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax3.tick_params(length=0)
ax3.set_xlim(0,3000,500)

#2005
ax4.hist(mmr_2005['MMR'], color='darkred', edgecolor='white')
ax4.set_title('2005 MMR Distribution', fontweight='bold', loc='left')
ax4.spines['left'].set_visible(False)
ax4.spines['top'].set_visible(False)
ax4.spines['right'].set_visible(False)
ax4.spines['bottom'].set_visible(False)
ax4.tick_params(length=0)
ax4.set_xlim(0,3000,500)

#2010
ax5.hist(mmr_2010['MMR'], color='darkred', edgecolor='white')
ax5.set_title('2010 MMR Distribution', fontweight='bold', loc='left')
ax5.spines['left'].set_visible(False)
ax5.spines['top'].set_visible(False)
ax5.spines['right'].set_visible(False)
ax5.spines['bottom'].set_visible(False)
ax5.tick_params(length=0)
ax5.set_xlim(0,3000,500)

#2015
ax6.hist(mmr_2015['MMR'], color='darkred', edgecolor='white')
ax6.set_title('2015 MMR Distribution', fontweight='bold', loc='left')
ax6.spines['left'].set_visible(False)
ax6.spines['top'].set_visible(False)
ax6.spines['right'].set_visible(False)
ax6.spines['bottom'].set_visible(False)
ax6.tick_params(length=0)
ax6.set_xlim(0,3000,500)
ax6.text(600, 50, 'Note how the bins \nshifted left over time.', size=12)

The output should look like this: enter image description here

The data is from UNICEF, under Access the Data https://data.unicef.org/topic/maternal-health/maternal-mortality/


Solution

  • You could create a list with the years and iterate over it:

    years = ['1990', '1995', '2000', '2005', '2010', '2015']
    
    plot_i = 1
    for y in years:
      data = mmr[mmr['Year']==y]
      ax = fig.add_subplot(2,3,plot_i)
      ax.hist(data['MMR'], color='darkred', edgecolor='white')
      ax.set_title(y + ' MMR Distribution', fontweight='bold', loc='left')
      ax.spines['left'].set_visible(False)
      ax.spines['top'].set_visible(False)
      ax.spines['right'].set_visible(False)
      ax.spines['bottom'].set_visible(False)
      ax.tick_params(length=0)
      ax.set_xlim(0,3000,500)
      plot_i = plot_i + 1