Skip to content

Commit ca01a53

Browse files
committed
DOC
1 parent a62c549 commit ca01a53

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
`~.axes.Axes.set_title` gains a y keyword argument to control auto positioning
2+
------------------------------------------------------------------------------
3+
`~.axes.Axes.set_title` tries to auto-position the title to avoid any
4+
decorators on the top x-axis. This is not always desirable so now
5+
*y* is an explicit keyword argument of `~.axes.Axes.set_title`. It
6+
defaults to *None* which means to use auto-positioning. If a value is
7+
supplied (i.e. the pre-3.0 default was ``y=1.0``) then auto-positioning is
8+
turned off. This can also be set with the new rcParameter :rc:`axes.titley`.

examples/text_labels_and_annotations/titles_demo.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
===========
3-
Titles Demo
4-
===========
2+
=================
3+
Title positioning
4+
=================
55
6-
matplotlib can display plot titles centered, flush with the left side of
6+
Matplotlib can display plot titles centered, flush with the left side of
77
a set of axes, and flush with the right side of a set of axes.
88
99
"""
@@ -16,3 +16,45 @@
1616
plt.title('Right Title', loc='right')
1717

1818
plt.show()
19+
20+
###########################################################################
21+
# The vertical position is automatically chosen to avoid decorators on the
22+
# topmost x-axis:
23+
24+
fig, axs = plt.subplots(1, 2, constrained_layout=True)
25+
26+
ax = axs[0]
27+
ax.plot(range(10))
28+
ax.xaxis.set_label_position('top')
29+
ax.set_xlabel('X-label')
30+
ax.set_title('Center Title')
31+
32+
ax = axs[1]
33+
ax.plot(range(10))
34+
ax.xaxis.set_label_position('top')
35+
ax.xaxis.tick_top()
36+
ax.set_xlabel('X-label')
37+
ax.set_title('Center Title')
38+
plt.show()
39+
40+
###########################################################################
41+
# Automatic positioning can be turned off by manually specifying the
42+
# *y* kwarg for the title or setting :rc:`axes.titley` in the rcParams.
43+
44+
fig, axs = plt.subplots(1, 2, constrained_layout=True)
45+
46+
ax = axs[0]
47+
ax.plot(range(10))
48+
ax.xaxis.set_label_position('top')
49+
ax.set_xlabel('X-label')
50+
ax.set_title('Manual y', y=1.0, pad=-14)
51+
52+
plt.rcParams['axes.titley'] = 1.0 # y is in axes-relative co-ordinates.
53+
# 1.0 is top of axes.
54+
plt.rcParams['axes.titlepad'] = -14 # pad is in points...
55+
ax = axs[1]
56+
ax.plot(range(10))
57+
ax.set_xlabel('X-label')
58+
ax.set_title('rcParam y')
59+
60+
plt.show()

0 commit comments

Comments
 (0)