I'm trying to define a function that can plot subplots within a 4-by-n grid. The code below works but I want to make the subplots share y axis, which I think I (definitely?) have to use the method mentioned in this post. However, because I'm doing a 4-by-n grid and I'm doing a loop, assigning a name to each subplot seems troublesome to me. Any ideas? Thank you in advance!
Currently my function looks like this:
def make_plots(df, name_list):
n = len(name_list)
j = 1
plt.figure(1, dpi=500, figsize=(10,10/4*n/4))
for i in name_list:
plt.subplot(n/4,4,j)
plt.title('Table {}: {}'.format(j,i), size='xx-small')
plt.grid(False)
a = [i for i in list(df[i]) if i>0]
plt.hist(a,bins=7,rwidth=0.7)
plt.axvline(x=np.mean(a), color='red')
j+=1
plt.tight_layout(pad=0.2,h_pad=0.2, w_pad=0.2)
plt.show()