Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my xlabel not show up? (It's not getting cut off)

I'm making a plot and for some reason my xlabel is not showing up. I don't think it is getting cut off, because when I call tight_layout it still doesn't show up. Any idea what is causing this issue? Here is the code used to generate the figure with some made up data.

import matplotlib.pyplot as plt
import numpy as np


fig, ax = plt.subplots(figsize=(8,5))

Months = ['May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct']

P = [220, 120, 50, 24, 54, 72]
T = [7, 12, 18, 24, 14, 5]

ax.bar(np.arange(1.5, len(P)+1.5), P, 0.5, label='Precipitation', color='k');
ax.set_ylabel("Precipitation, mm", fontsize=12)
ax.legend(loc=2, frameon=False, fontsize=12)

ax2 = ax.twinx()
ax2.plot(np.arange(1.75, len(P)+1.75), T, label='Air Temperature');
ax2.set_ylabel(r'Air Temperature, $^{o}$C', fontsize=12)
ax2.legend(loc=1, frameon=False, fontsize=12)
ax2.set_ylim(0,30)

plt.xticks(np.arange(1.75, len(P)+1.75), Months)
plt.xlim(1, 7.5)
plt.xlabel("2013", fontsize=12)

plt.tight_layout()

And an image of the figure itself (With the xlabel not present).

enter image description here

like image 833
pbreach Avatar asked May 03 '15 21:05

pbreach


People also ask

How do you not show Xlabel?

To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.

How do you show all the Xticks?

Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot. To display the figure, use show() method.

What does plt tight_ layout do?

tight_layout automatically adjusts subplot params so that the subplot(s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.


1 Answers

Using

ax.set_xlabel("2013", fontsize=12)`

instead of

plt.xlabel("2013", fontsize=12)

works for me.

like image 145
FriendFX Avatar answered Oct 24 '22 20:10

FriendFX