Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 55fc151

Browse filesBrowse files
pwuertzpwuertz
pwuertz
authored andcommitted
backend_pgf: implement anchoring of text elements
1 parent c53316f commit 55fc151
Copy full SHA for 55fc151

File tree

Expand file treeCollapse file tree

1 file changed

+32
-56
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-56
lines changed

‎lib/matplotlib/backends/backend_pgf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pgf.py
+32-56Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -599,26 +599,49 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
599599
self.draw_text(gc, x, y, s, prop, angle, ismath, mtext)
600600

601601
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
602+
# prepare string for tex
602603
s = common_texification(s)
603-
604-
# apply font properties
605604
prop_cmds = _font_properties_str(prop)
606605
s = ur"{%s %s}" % (prop_cmds, s)
607606

608-
# draw text at given coordinates
609-
x = x * 1. / self.dpi
610-
y = y * 1. / self.dpi
607+
611608
writeln(self.fh, r"\begin{pgfscope}")
609+
612610
alpha = gc.get_alpha()
613611
if alpha != 1.0:
614612
writeln(self.fh, r"\pgfsetfillopacity{%f}" % alpha)
615613
writeln(self.fh, r"\pgfsetstrokeopacity{%f}" % alpha)
616-
stroke_rgb = tuple(gc.get_rgb())[:3]
617-
if stroke_rgb != (0, 0, 0):
618-
writeln(self.fh, r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % stroke_rgb)
614+
rgb = tuple(gc.get_rgb())[:3]
615+
if rgb != (0, 0, 0):
616+
writeln(self.fh, r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % rgb)
619617
writeln(self.fh, r"\pgfsetstrokecolor{textcolor}")
620618
writeln(self.fh, r"\pgfsetfillcolor{textcolor}")
621-
writeln(self.fh, "\\pgftext[left,bottom,x=%fin,y=%fin,rotate=%f]{%s}\n" % (x, y, angle, s))
619+
620+
f = 1.0 / self.figure.dpi
621+
text_args = []
622+
if angle == 0 or mtext.get_rotation_mode() == "anchor":
623+
# if text anchoring can be supported, get the original coordinates
624+
# and add alignment information
625+
x, y = mtext.get_transform().transform_point(mtext.get_position())
626+
text_args.append("x=%fin" % (x * f))
627+
text_args.append("y=%fin" % (y * f))
628+
629+
halign = {"left": "left", "right": "right", "center": ""}
630+
valign = {"top": "top", "bottom": "bottom",
631+
"baseline": "base", "center": ""}
632+
text_args.append(halign[mtext.get_ha()])
633+
text_args.append(valign[mtext.get_va()])
634+
else:
635+
# if not, use the text layout provided by matplotlib
636+
text_args.append("x=%fin" % (x * f))
637+
text_args.append("y=%fin" % (y * f))
638+
text_args.append("left")
639+
text_args.append("bottom")
640+
641+
if angle != 0:
642+
text_args.append("rotate=%f" % angle)
643+
644+
writeln(self.fh, r"\pgftext[%s]{%s}" % (",".join(text_args), s))
622645
writeln(self.fh, r"\end{pgfscope}")
623646

624647
def get_text_width_height_descent(self, s, prop, ismath):
@@ -861,53 +884,6 @@ def print_png(self, fname_or_fh, *args, **kwargs):
861884
else:
862885
raise ValueError("filename must be a path or a file-like object")
863886

864-
def _render_texts_pgf(self, fh):
865-
# TODO: currently unused code path
866-
867-
# alignment anchors
868-
valign = {"top": "top", "bottom": "bottom", "baseline": "base", "center": ""}
869-
halign = {"left": "left", "right": "right", "center": ""}
870-
# alignment anchors for 90deg. rotated labels
871-
rvalign = {"top": "left", "bottom": "right", "baseline": "right", "center": ""}
872-
rhalign = {"left": "top", "right": "bottom", "center": ""}
873-
874-
# TODO: matplotlib does not hide unused tick labels yet, workaround
875-
for tick in self.figure.findobj(mpl.axis.Tick):
876-
tick.label1.set_visible(tick.label1On)
877-
tick.label2.set_visible(tick.label2On)
878-
# TODO: strange, first legend label is always "None", workaround
879-
for legend in self.figure.findobj(mpl.legend.Legend):
880-
labels = legend.findobj(mpl.text.Text)
881-
labels[0].set_visible(False)
882-
# TODO: strange, legend child labels are duplicated,
883-
# find a list of unique text objects as workaround
884-
texts = self.figure.findobj(match=Text, include_self=False)
885-
texts = list(set(texts))
886-
887-
# draw text elements
888-
for text in texts:
889-
s = text.get_text()
890-
if not s or not text.get_visible():
891-
continue
892-
893-
s = common_texification(s)
894-
895-
fontsize = text.get_fontsize()
896-
angle = text.get_rotation()
897-
transform = text.get_transform()
898-
x, y = transform.transform_point(text.get_position())
899-
x = x * 1.0 / self.figure.dpi
900-
y = y * 1.0 / self.figure.dpi
901-
# TODO: positioning behavior unknown for rotated elements
902-
# right now only the alignment for 90deg rotations is correct
903-
if angle == 90.:
904-
align = rvalign[text.get_va()] + "," + rhalign[text.get_ha()]
905-
else:
906-
align = valign[text.get_va()] + "," + halign[text.get_ha()]
907-
908-
s = ur"{\fontsize{%f}{%f}\selectfont %s}" % (fontsize, fontsize*1.2, s)
909-
writeln(fh, ur"\pgftext[%s,x=%fin,y=%fin,rotate=%f]{%s}" % (align,x,y,angle,s))
910-
911887
def get_renderer(self):
912888
return RendererPgf(self.figure, None)
913889

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.