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


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/





You can ignore the year_list. I put that there for the loop. It's not utilized in my current code.
– Drew M
Jul 2 at 3:41





You might want to take this to codereview: codereview.stackexchange.com
– Matthew Story
Jul 2 at 3:43




3 Answers
3



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





Note this drops the ax6.text(600, 50, 'Note how the bins nshifted left over time.', size=12) line
– Dillon Davis
Jul 2 at 4:00


ax6.text(600, 50, 'Note how the bins nshifted left over time.', size=12)



Since you set all the ax to false you can do:


axes = [ax1, ax2, ax3, ax4, ax5, ax6]
for ax in axes:
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.set_xlim(0,3000,500)



You are probably looking for a loop similar to this.


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

axes_list =

year_names = ["1990", "1995", "2000", "2005", "2010", "2015"]

for num, year in enumerate(year_names):
MMR = mmr[mmr['Year']==year]
axis = fig.add_subplot(2,3,num)
axes_list.append(axis)
axis.hist(MMR['MMR'], color='darkred', edgecolor='white')
axis.set_title('{0} MMR Distribution'.format(year), fontweight='bold', loc='left')
axis.spines['left'].set_visible(False)
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.spines['bottom'].set_visible(False)
axis.tick_params(length=0)
axis.set_xlim(0,3000,500)

axes_list[-1].text(600, 50, 'Note how the bins nshifted left over time.', size=12)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages