Pandas / Matplotlib Histogram Issues - x-axis ticks disappear with subplots and extra unnecessary space for maps. Tried tight_layout and subplots_adjust
I have several problems with plotting histogram graphs in Matplotlib (it looks like a histogram, same with bars and line graphs, doesn't seem to have this issue):
-
I am unable to get the x-axis labels to display on my top 2 charts. In my code below, I never once set subplots to share the x-axis or whatever. I've also tried this with
plt.setp(ax.get_xticklabels(), visible=True)
, but that 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 noticed that if I only draw the top left histogram, the check marks on the x axis actually appear, but disappear as soon as I draw more.
I also tried adjusting tight_layout, plt.tight_layout(w_pad = 2, h_pad = 2)
but that doesn't show my checkbox values ββon the x axis.
- subplot seems to leave extra space in some plots on the right side. How do I get rid of this space and use the entire x-axis for my histogram?
This is what it all looks like it is.
Why do some of the x-axis ticks automatically appear and some do not? And why do my cards have so much extra space? This is not a problem when I made a histogram instead of a histogram ... (note that I tried to tweak hspace
and wspace
in subplot_adjust
).
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()
source to share