I am having a couple issues plotting subplots in Matplotlib:
I can't get the x-axis tick labels to show up on my top 2 charts. In my code below, I haven't once specified the subplots to share x-axis or anything. I've also tried this with
plt.setp(ax.get_xticklabels(), visible=True)
as well, but it doesn't change the lack of x-axis labels one bit.plt.subplot2grid((6,4), [0,0], 2, 2) ax = firstperiod.megaball.plot(kind='hist', bins = 25) plt.setp(ax.get_xticklabels(), visible=True) plt.xticks(range(0,26,5), range(0,26,5), rotation="horizontal") plt.title('Megaball Distrib \'96 - \'99') plt.ylabel("# of draws", fontsize = 10)
I've noticed that if I plot just the top left histogram, the x-axis ticks actually show up, but disappear as soon as I plot more.
I've tried adjusting tight_layout as well, plt.tight_layout(w_pad = 2, h_pad = 2)
, but that doesn't help show my x-axis tick values.
- subplot seems to leave extra space in some of the charts on the right hand side. How do I get rid of that space and use the whole x-axis for my histogram?
Here is what it the whole thing looks like as is.
Why are some of the x-axis ticks automatically showing up, and some not? And why do my charts have so much extra space? This isn't an issue when I made a bar chart instead of a histogram... (notice that I've tried adjusting hspace
and wspace
in subplot_adjust
as well).
fig = plt.figure()
fig.suptitle('Distribution of MegaBall Draws', fontsize=20)
plt.subplot2grid((6,4), [0,0], 2, 2)
ax = firstperiod.megaball.plot(kind='hist', bins = 25)
plt.setp(ax.get_xticklabels(), visible=True)
plt.xticks(range(0,26,5), range(0,26,5), rotation="horizontal")
plt.title('Megaball Distrib \'96 - \'99')
plt.ylabel("# of draws", fontsize = 10)
plt.subplot2grid((6,4), [0,2], 2, 2)
secondperiod.megaball.plot(kind='hist', bins = 36)
plt.xticks(range(0,36,5), range(0,41,5), rotation="horizontal")
plt.title('Megaball Distrib \'99 - \'02')
plt.ylabel("# of draws", fontsize = 10)
plt.subplot2grid((6,4), [2,0], 2, 2)
thirdperiod.megaball.plot(kind='hist', bins = 52)
plt.xticks(range(0,55,5), range(0,55,5), rotation="horizontal")
plt.title('Megaball Distrib \'02 - \'05')
plt.ylabel("# of draws", fontsize = 10)
plt.subplot2grid((6,4), [2,2], 2, 2)
fourthperiod.megaball.plot(kind='hist', bins = 46)
plt.xticks(range(0,50,5), range(0,50,5),rotation="horizontal")
plt.title('Megaball Distrib \'05 - \'13')
plt.ylabel("# of draws", fontsize = 10)
plt.subplot2grid((6,4), [4,1], 2, 2)
fifthperiod.megaball.plot(kind='hist', bins = 15)
plt.xticks(rotation="horizontal")
plt.title('Megaball Distrib \'13 - ')
plt.ylabel("# of draws", fontsize = 10)
plt.tight_layout(w_pad = 2, h_pad = 2)
plt.subplots_adjust(top = 0.8, wspace = 0.75, hspace = 2.5)
plt.savefig("megaball_distribs.png")
plt.show()