Skip to content

Commit 527f7c4

Browse files
authored
Merge pull request matplotlib#22293 from timhoffm/doc-tick-top
Modify example for x-axis tick labels at the top
2 parents a6da11e + 9e87dc8 commit 527f7c4

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

examples/ticks/tick_xlabel_top.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
"""
2-
==========================================
3-
Set default x-axis tick labels on the top
4-
==========================================
2+
==================================
3+
Move x-axis tick labels to the top
4+
==================================
55
6-
We can use :rc:`xtick.labeltop` and :rc:`xtick.top` and :rc:`xtick.labelbottom`
7-
and :rc:`xtick.bottom` to control where on the axes ticks and their labels
8-
appear.
6+
`~.axes.Axes.tick_params` can be used to configure the ticks. *top* and
7+
*labeltop* control the visibility tick lines and labels at the top x-axis.
8+
To move x-axis ticks from bottom to top, we have to activate the top ticks
9+
and deactivate the bottom ticks::
910
10-
These properties can also be set in ``.matplotlib/matplotlibrc``.
11-
"""
11+
ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
1212
13-
import matplotlib.pyplot as plt
14-
import numpy as np
13+
.. note::
14+
15+
If the change should be made for all future plots and not only the current
16+
Axes, you can adapt the respective config parameters
1517
18+
- :rc:`xtick.top`
19+
- :rc:`xtick.labeltop`
20+
- :rc:`xtick.bottom`
21+
- :rc:`xtick.labelbottom`
1622
17-
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
18-
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
23+
"""
1924

20-
x = np.arange(10)
25+
import matplotlib.pyplot as plt
2126

2227
fig, ax = plt.subplots()
23-
24-
ax.plot(x)
25-
ax.set_title('xlabel top') # Note title moves to make room for ticks
28+
ax.plot(range(10))
29+
ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
30+
ax.set_title('x-ticks moved to the top')
2631

2732
plt.show()

0 commit comments

Comments
 (0)