Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 917f3ae

Browse files
author
Andrew Dawson
committed
NF - Left and right side axes titles
Add left and right side titles to a set of axes, in addition to the existing title which is in the center.
1 parent 1efa832 commit 917f3ae

File tree

13 files changed

+1064
-40
lines changed

13 files changed

+1064
-40
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2013-01-08 Added two extra titles to axes which are flush with the left and
2+
right edges of the plot respectively.
3+
Andrew Dawson
4+
15
2013-01-07 Add framealpha keyword argument to legend - PO
26

37
2012-12-22 Added classes for interpolation within triangular grids

doc/users/whats_new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Ian Thomas added classes to perform interpolation within triangular grids
5656
the triangles in which points lie (
5757
:class:`~matplotlib.tri.TrapezoidMapTriFinder`).
5858

59+
Left and right side axes titles
60+
-------------------------------
61+
Andrew Dawson added the ability to add axes titles flush with the left and
62+
right sides of the top of the axes using a new keyword argument `loc` to
63+
:func:`~matplotlib.pyplot.title`.
64+
5965
.. _whats-new-1-2:
6066

6167
new in matplotlib-1.2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
"""
3+
matplotlib can display plot titles centered, flush with the left side of
4+
a set of axes, and flush with the right side of a set of axes.
5+
6+
"""
7+
import matplotlib.pyplot as plt
8+
9+
plt.plot(range(10))
10+
11+
plt.title('Center Title')
12+
plt.title('Left Title', loc='left')
13+
plt.title('Right Title', loc='right')
14+
15+
plt.show()

lib/matplotlib/axes.py

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,23 @@ def cla(self):
932932
verticalalignment='baseline',
933933
horizontalalignment='center',
934934
)
935-
self.title.set_transform(self.transAxes + self.titleOffsetTrans)
936-
self.title.set_clip_box(None)
935+
self._left_title = mtext.Text(
936+
x=0.0, y=1.0, text='',
937+
fontproperties=props,
938+
verticalalignment='baseline',
939+
horizontalalignment='left',
940+
)
941+
self._right_title = mtext.Text(
942+
x=1.0, y=1.0, text='',
943+
fontproperties=props,
944+
verticalalignment='baseline',
945+
horizontalalignment='right',
946+
)
937947

938-
self._set_artist_props(self.title)
948+
for _title in (self.title, self._left_title, self._right_title):
949+
_title.set_transform(self.transAxes + self.titleOffsetTrans)
950+
_title.set_clip_box(None)
951+
self._set_artist_props(_title)
939952

940953
# the patch draws the background of the axes. we want this to
941954
# be below the other artists; the axesPatch name is
@@ -1990,6 +2003,8 @@ def draw(self, renderer=None, inframe=False):
19902003
artists.extend([self.xaxis, self.yaxis])
19912004
if not inframe:
19922005
artists.append(self.title)
2006+
artists.append(self._left_title)
2007+
artists.append(self._right_title)
19932008
artists.extend(self.tables)
19942009
if self.legend_ is not None:
19952010
artists.append(self.legend_)
@@ -3100,6 +3115,8 @@ def get_children(self):
31003115
children.append(self.legend_)
31013116
children.extend(self.collections)
31023117
children.append(self.title)
3118+
children.append(self._left_title)
3119+
children.append(self._right_title)
31033120
children.append(self.patch)
31043121
children.extend(self.spines.itervalues())
31053122
return children
@@ -3137,43 +3154,82 @@ def pick(self, *args):
31373154

31383155
### Labelling
31393156

3140-
def get_title(self):
3141-
"""
3142-
Get the title text string.
3143-
"""
3144-
return self.title.get_text()
3145-
3146-
@docstring.dedent_interpd
3147-
def set_title(self, label, fontdict=None, **kwargs):
3148-
"""
3149-
Call signature::
3157+
def get_title(self, loc="center"):
3158+
"""Get an axes title.
31503159
3151-
set_title(label, fontdict=None, **kwargs):
3160+
Get one of the three available axes titles. The available titles
3161+
are positioned above the axes in the center, flush with the left
3162+
edge, and flush with the right edge.
31523163
3153-
Set the title for the axes.
3164+
Parameters
3165+
----------
3166+
loc : {'center', 'left', 'right'}, str, optional
3167+
Which title to get, defaults to 'center'
31543168
3155-
kwargs are Text properties:
3156-
%(Text)s
3169+
Returns
3170+
-------
3171+
text : :class:`~matplotlib.text.Text`
3172+
The matplotlib text instance representing the title
31573173
3158-
ACCEPTS: str
3174+
"""
3175+
try:
3176+
title = {'left': self._left_title,
3177+
'center': self.title,
3178+
'right': self._right_title}[loc.lower()]
3179+
except KeyError:
3180+
raise ValueError("'%s' is not a valid location" % loc)
3181+
return title
31593182

3160-
.. seealso::
3183+
@docstring.dedent_interpd
3184+
def set_title(self, label, loc="center", fontdict=None, **kwargs):
3185+
"""Set a title for the axes.
3186+
3187+
Set one of the three available axes titles. The available titles
3188+
are positioned above the axes in the center, flush with the left
3189+
edge, and flush with the right edge.
3190+
3191+
Parameters
3192+
----------
3193+
label : str
3194+
Text to use for the title
3195+
loc : {'center', 'left', 'right'}, str, optional
3196+
Which title to set, defaults to 'center'
3197+
fontdict : dict
3198+
A dictionary controlling the appearance of the title text,
3199+
the default `fontdict` is:
3200+
{'fontsize': rcParams['axes.titlesize'],
3201+
'verticalalignment': 'baseline',
3202+
'horizontalalignment': loc}
3203+
3204+
Returns
3205+
-------
3206+
text : :class:`~matplotlib.text.Text`
3207+
The matplotlib text instance representing the title
3208+
3209+
Other parameters
3210+
----------------
3211+
Other keyword arguments are text properties, see
3212+
:class:`~matplotlib.text.Text` for a list of valid text
3213+
properties.
31613214
3162-
:meth:`text`
3163-
for information on how override and the optional args work
31643215
"""
3216+
try:
3217+
title = {'left': self._left_title,
3218+
'center': self.title,
3219+
'right': self._right_title}[loc.lower()]
3220+
except KeyError:
3221+
raise ValueError("'%s' is not a valid location" % loc)
31653222
default = {
31663223
'fontsize': rcParams['axes.titlesize'],
31673224
'verticalalignment': 'baseline',
3168-
'horizontalalignment': 'center'
3225+
'horizontalalignment': loc.lower()
31693226
}
3170-
3171-
self.title.set_text(label)
3172-
self.title.update(default)
3227+
title.set_text(label)
3228+
title.update(default)
31733229
if fontdict is not None:
3174-
self.title.update(fontdict)
3175-
self.title.update(kwargs)
3176-
return self.title
3230+
title.update(fontdict)
3231+
title.update(kwargs)
3232+
return title
31773233

31783234
def get_xlabel(self):
31793235
"""
@@ -8839,6 +8895,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
88398895

88408896
if self.title.get_visible():
88418897
bb.append(self.title.get_window_extent(renderer))
8898+
if self._left_title.get_visible():
8899+
bb.append(self._left_title.get_window_extent(renderer))
8900+
if self._right_title.get_visible():
8901+
bb.append(self._right_title.get_window_extent(renderer))
88428902

88438903
bb_xaxis = self.xaxis.get_tightbbox(renderer)
88448904
if bb_xaxis:

lib/matplotlib/pyplot.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,28 +1171,51 @@ def box(on=None):
11711171
ax.set_frame_on(on)
11721172
draw_if_interactive()
11731173

1174+
11741175
def title(s, *args, **kwargs):
11751176
"""
1176-
Set the title of the current axis.
1177+
title(label, loc='center', fontdict=None, **kwargs)
11771178
1178-
Default font override is::
1179+
Set a title of the current axes.
11791180
1180-
override = {'fontsize': 'medium',
1181-
'verticalalignment': 'baseline',
1182-
'horizontalalignment': 'center'}
1181+
Set one of the three available axes titles. The available titles are
1182+
positioned above the axes in the center, flush with the left edge,
1183+
and flush with the right edge.
11831184
1184-
.. seealso::
1185+
Parameters
1186+
----------
1187+
label : str
1188+
Text to use for the title
1189+
loc : {'center', 'left', 'right'}, str, optional
1190+
Which title to set, defaults to 'center'
1191+
fontdict : dict
1192+
A dictionary controlling the appearance of the title text,
1193+
the default `fontdict` is:
1194+
{'fontsize': rcParams['axes.titlesize'],
1195+
'verticalalignment': 'baseline',
1196+
'horizontalalignment': loc}
1197+
1198+
Returns
1199+
-------
1200+
text : :class:`~matplotlib.text.Text`
1201+
The matplotlib text instance representing the title
1202+
1203+
Other parameters
1204+
----------------
1205+
Other keyword arguments are text properties, see
1206+
:class:`~matplotlib.text.Text` for a list of valid text
1207+
properties.
1208+
1209+
See also
1210+
--------
1211+
:func:`~matplotlib.pyplot.text` : Add text to the current axes
11851212
1186-
:func:`~matplotlib.pyplot.text`
1187-
for information on how override and the optional args work.
11881213
"""
11891214
l = gca().set_title(s, *args, **kwargs)
11901215
draw_if_interactive()
11911216
return l
11921217

11931218

1194-
1195-
11961219
## Axis ##
11971220

11981221
def axis(*v, **kwargs):
Binary file not shown.
Loading

0 commit comments

Comments
 (0)