有人可以帮我用 for 循环或函数重写这段代码(带有多个直方图的图)吗?

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

我看过一个类似的 post,但我仍在努力解决它。如果你不想,你不必写出整个代码。但是句法指南会有所帮助。这是我冗长的手动方法:

# 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)

输出应如下所示: enter image description here

数据来自联合国儿童基金会,在访问数据下https://data.unicef.org/topic/maternal-health/maternal-mortality/

既然你把所有的斧头都设置为 false 你可以这样做:

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)

您可以创建一个包含年份的列表并对其进行迭代:

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

您可能正在寻找与此类似的循环。

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)