1

I have a graph with two x axis label, top and bottom. The top label is running into the title of the graph. Is there anyway to put labels on the inside of the graph? I don't see it in the documentation.

The alternative is to use a raised title, but I can't do that because my graphs are stacked. The raised title from graph below will get mixed up with the graph above it:

Python Matplotlib figure title overlaps axes label when using twiny

Community
  • 1
  • 1
jason
  • 3,811
  • 18
  • 92
  • 147

1 Answers1

2

you can move the location of axis labels using set_label_coords.

The coords you give it are x and y, and by default the transform is the axes coordinate system: so (0,0) is (left,bottom), (0.5, 0.5) is in the middle, etc.

So, an x coord of 0.5 centres the text, and a y coord of 0.95 brings the label inside the plot, below the top axis.

Here's a quick example code to show how to do it:

import matplotlib.pyplot as plt

fig=plt.figure()
ax1=fig.add_subplot(111)
ax2=ax1.twiny()

ax1.set_xlabel('xlabel 1')
ax2.set_xlabel('xlabel 2')

ax2.xaxis.set_label_coords(0.5,0.95)

ax1.set_title('my title')

plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • very helpful, based on your answer, i tried the following and it worked. `ax2.set_xticklabels(xlabels, fontsize=11,y=0.85)` – jason Oct 19 '15 at 09:59