|
| 1 | +""" |
| 2 | +=============================================== |
| 3 | +Move x/y-axis ticks and labels on top and right |
| 4 | +=============================================== |
| 5 | +
|
| 6 | +`.axes.Axes.tick_params` can be used to move tick marks and tick labels |
| 7 | +to the top or right side of the Axes:: |
| 8 | +
|
| 9 | + ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False) |
| 10 | + ax.tick_params(right=True, labelright=True, left=False, labelleft=False) |
| 11 | +
|
| 12 | +To also reposition the axes labels (xlabel/ylabel), use |
| 13 | +``Axes.xaxis.set_label_position`` and ``Axes.yaxis.set_label_position``. |
| 14 | +
|
| 15 | +.. note:: |
| 16 | +
|
| 17 | + To apply this globally for all future plots, set the rcParams: |
| 18 | +
|
| 19 | + - :rc:`xtick.top` |
| 20 | + - :rc:`xtick.labeltop` |
| 21 | + - :rc:`xtick.bottom` |
| 22 | + - :rc:`xtick.labelbottom` |
| 23 | + - :rc:`ytick.right` |
| 24 | + - :rc:`ytick.labelright` |
| 25 | + - :rc:`ytick.left` |
| 26 | + - :rc:`ytick.labelleft` |
| 27 | +
|
| 28 | +.. redirect-from:: /gallery/ticks/tick_xlabel_top |
| 29 | +.. redirect-from:: /gallery/ticks/tick_label_right |
| 30 | +""" |
| 31 | + |
| 32 | +import matplotlib.pyplot as plt |
| 33 | + |
| 34 | +fig, ax = plt.subplots() |
| 35 | +ax.plot(range(10)) |
| 36 | + |
| 37 | +# Move ticks marks and tick labels |
| 38 | +ax.tick_params( |
| 39 | + top=True, labeltop=True, |
| 40 | + bottom=False, labelbottom=False, |
| 41 | + right=True, labelright=True, |
| 42 | + left=False, labelleft=False, |
| 43 | +) |
| 44 | + |
| 45 | +# Move axis labels |
| 46 | +ax.xaxis.set_label_position("top") |
| 47 | +ax.yaxis.set_label_position("right") |
| 48 | + |
| 49 | +ax.set_xlabel("X label") |
| 50 | +ax.set_ylabel("Y label") |
| 51 | + |
| 52 | +plt.show() |
0 commit comments