10

In matplotlib what is the way to have tick labels both at the bottom and in the top x axis? I have searched a lot and still can't find how to do it.

elyase
  • 37,104
  • 11
  • 102
  • 114
  • Check this post: http://stackoverflow.com/questions/11410796/turn-off-the-the-upper-right-axis-tick-marks – CT Zhu Sep 15 '13 at 02:36
  • @CTZhu, yep I had found it but it seems to be related to changing the position of the xaxis to the top. – elyase Sep 15 '13 at 02:38

2 Answers2

11

Sorry, I lied in the comments. You can do this easily (but it seems to be badly documented)

fig, ax = plt.subplots(1, 1)
ax.xaxis.set_tick_params(labeltop='on')

output

tacaswell
  • 79,602
  • 19
  • 200
  • 189
  • Yes this is the best way! I wonder why there are any documentation about it!I link [this question](http://stackoverflow.com/questions/1992640/vertical-xtick-labels-on-top-not-bottom) too. – G M Sep 15 '13 at 16:31
4

You can do it with twiny():

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X2tick_location= ax1.xaxis.get_ticklocs() #Get the tick locations in data coordinates as a numpy array
ax2.set_xticks(X2tick_location)
ax2.set_xticklabels(X2tick_location)
plt.show()

enter image description here

Have a look to this question too for more elaborate plots.

Community
  • 1
  • 1
G M
  • 17,694
  • 10
  • 75
  • 78