0

I am having a couple issues plotting subplots in Matplotlib:

  1. 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.

enter image description here

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.

  1. 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.

enter image description here

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()
halfer
  • 19,824
  • 17
  • 99
  • 186
SpicyClubSauce
  • 4,076
  • 13
  • 37
  • 62
  • 1
    Does this answer it for you (`xlabel()` and `ylabel()`)? http://stackoverflow.com/a/12444777/1716866 – leekaiinthesky Jun 04 '15 at 05:29
  • 2
    What do you mean with x-axis label? Do you mean xlabel? ... I don't see any ... Do you mean the size of the xticklabels? Or is the range of the x-axis not consistent for the different plots? Please be more specific! – plonser Jun 04 '15 at 07:07
  • I agree with @plonser: Your question is unclear. And in the image you posted all font sizes seem to be consistent. – hitzg Jun 04 '15 at 08:22
  • @plonser - adjusted my question, any ideas? thanks in advance – SpicyClubSauce Jun 04 '15 at 15:38
  • When I use `plt.plot(...)` instead of `firstperiod.megaball.plot` it works for me, the xticklabels appear as expected. So, is there something odd in your member function `plot`? – plonser Jun 05 '15 at 07:02
  • http://stackoverflow.com/questions/27083051/matplotlib-xticks-not-lining-up-with-histogram – Amen Aug 11 '15 at 18:05

1 Answers1

-2

You can adjust the font size with the bizarrely named fontsize parameter.

plt.title('Megaball Distrib \'96-99', fontsize = 12)
maxymoo
  • 35,286
  • 11
  • 92
  • 119