Skip to content

Commit 9e87dc8

Browse files
committed
Modify example for x-axis tick labels at the top
Primarily describe how to do this for a single Axes (using `tick_params`). Only mention the `rcParams` in a note. Globally changing `rcParams` is usually not a good idea and should not be demonstrated. The whole topic of `rcParams` (global, context, matplotlibrc) is too long to be discussed here and not the focus of the example.
1 parent 075067c commit 9e87dc8

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

examples/ticks/tick_xlabel_top.py

+21-16
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)