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

Skip to content

Commit 4ef9115

Browse files
committed
Merge pull request #1081 from pwuertz/draw_text_with_mtext
Propagate mpl.text.Text instances to the backends and fix documentation
2 parents dd92a43 + c814997 commit 4ef9115

16 files changed

+116
-96
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2012-11-27 Added the *mtext* parameter for supplying matplotlib.text.Text
2+
instances to RendererBase.draw_tex and RendererBase.draw_text.
3+
This allows backends to utilize additional text attributes, like
4+
the alignment of text elements. - pwuertz
5+
16
2012-11-16 plt.set_cmap no longer throws errors if there is not already
27
an active colorable artist, such as an image, and just sets
38
up the colormap to use from that point forward. - PI

doc/users/whats_new.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ the whole figure. This was already the behavior for both
4141
:func:`~matplotlib.pyplot.axes` and :func:`~matplotlib.pyplot.subplots`, and
4242
now this consistency is shared with :func:`~matplotlib.pyplot.subplot`.
4343

44+
Anchored text support
45+
---------------------
46+
The `svg` and `pgf` backends are now able to save text alignment information
47+
to their output formats. This allows to edit text elements in saved figures,
48+
using Inkscape for example, while preserving their intended position. For
49+
`svg` please note that you'll have to disable the default text-to-path
50+
conversion (`mpl.rc('svg', fonttype='none')`).
51+
4452
.. _whats-new-1-2:
4553

4654
new in matplotlib-1.2

lib/matplotlib/axis.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,9 +1847,10 @@ def _get_label(self):
18471847
size=rcParams['axes.labelsize'],
18481848
weight=rcParams['axes.labelweight']),
18491849
color=rcParams['axes.labelcolor'],
1850-
verticalalignment='center',
1851-
horizontalalignment='right',
1850+
verticalalignment='bottom',
1851+
horizontalalignment='center',
18521852
rotation='vertical',
1853+
rotation_mode='anchor',
18531854
)
18541855
label.set_transform(mtransforms.blended_transform_factory(
18551856
mtransforms.IdentityTransform(), self.axes.transAxes))

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ def option_scale_image(self):
443443
"""
444444
return False
445445

446-
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!'):
446+
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
447447
"""
448448
"""
449449
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX")
450450

451-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
451+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
452452
"""
453453
Draw the text instance
454454
@@ -462,14 +462,17 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
462462
the y location of the text in display coords
463463
464464
*s*
465-
a :class:`matplotlib.text.Text` instance
465+
the text string
466466
467467
*prop*
468468
a :class:`matplotlib.font_manager.FontProperties` instance
469469
470470
*angle*
471471
the rotation angle in degrees
472472
473+
*mtext*
474+
a :class:`matplotlib.text.Text` instance
475+
473476
**backend implementers note**
474477
475478
When you are trying to determine if you have gotten your bounding box

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
158158
y = int(y) - oy
159159
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)
160160

161-
def draw_text(self, gc, x, y, s, prop, angle, ismath):
161+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
162162
"""
163163
Render the text
164164
"""
@@ -215,7 +215,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
215215
return w, h, d
216216

217217

218-
def draw_tex(self, gc, x, y, s, prop, angle):
218+
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
219219
# todo, handle props, angle, origins
220220
size = prop.get_size_in_points()
221221

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def draw_image(self, gc, x, y, im):
177177

178178
im.flipud_out()
179179

180-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
180+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
181181
# Note: x,y are device/display coords, not user-coords, unlike other
182182
# draw_* methods
183183
if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))

lib/matplotlib/backends/backend_emf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def draw_rectangle(self, gcEdge, rgbFace, x, y, width, height):
358358
if debugPrint: print("draw_rectangle: optimizing away (%f,%f) w=%f,h=%f" % (x,y,width,height))
359359

360360

361-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
361+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
362362
"""
363363
Draw the text.Text instance s at x,y (display coords) with font
364364
properties instance prop at angle in degrees, using GraphicsContext gc

lib/matplotlib/backends/backend_gdk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def draw_image(self, gc, x, y, im):
138138
im.flipud_out()
139139

140140

141-
def draw_text(self, gc, x, y, s, prop, angle, ismath):
141+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
142142
x, y = int(x), int(y)
143143

144144
if x < 0 or y < 0: # window has shrunk and text is off the edge

lib/matplotlib/backends/backend_macosx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def draw_image(self, gc, x, y, im):
111111
*gc.get_clip_path())
112112
im.flipud_out()
113113

114-
def draw_tex(self, gc, x, y, s, prop, angle):
114+
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
115115
# todo, handle props, angle, origins
116116
size = prop.get_size_in_points()
117117
texmanager = self.get_texmanager()
@@ -128,7 +128,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
128128
self.mathtext_parser.parse(s, self.dpi, prop)
129129
gc.draw_mathtext(x, y, angle, 255 - image.as_array())
130130

131-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
131+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
132132
if ismath:
133133
self._draw_mathtext(gc, x, y, s, prop, angle)
134134
else:

lib/matplotlib/backends/backend_pdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
16711671
# Pop off the global transformation
16721672
self.file.output(Op.grestore)
16731673

1674-
def draw_tex(self, gc, x, y, s, prop, angle):
1674+
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
16751675
texmanager = self.get_texmanager()
16761676
fontsize = prop.get_size_in_points()
16771677
dvifile = texmanager.make_dvi(s, fontsize)
@@ -1763,7 +1763,7 @@ def encode_string(self, s, fonttype):
17631763
return s.encode('cp1252', 'replace')
17641764
return s.encode('utf-16be', 'replace')
17651765

1766-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
1766+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
17671767
# TODO: combine consecutive texts into one BT/ET delimited section
17681768

17691769
# This function is rather complex, since there is no way to

0 commit comments

Comments
 (0)