MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / draw_text

Method draw_text

lib/matplotlib/backends/backend_pgf.py:686–732  ·  view source on GitHub ↗
(self, gc, x, y, s, prop, angle, ismath=False, mtext=None)

Source from the content-addressed store, hash-verified

684 self.draw_text(gc, x, y, s, prop, angle, ismath="TeX", mtext=mtext)
685
686 def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
687 # docstring inherited
688
689 # prepare string for tex
690 s = _escape_and_apply_props(s, prop)
691
692 _writeln(self.fh, r"\begin{pgfscope}")
693 self._print_pgf_clip(gc)
694
695 alpha = gc.get_alpha()
696 if alpha != 1.0:
697 _writeln(self.fh, r"\pgfsetfillopacity{%f}" % alpha)
698 _writeln(self.fh, r"\pgfsetstrokeopacity{%f}" % alpha)
699 rgb = tuple(gc.get_rgb())[:3]
700 _writeln(self.fh, r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % rgb)
701 _writeln(self.fh, r"\pgfsetstrokecolor{textcolor}")
702 _writeln(self.fh, r"\pgfsetfillcolor{textcolor}")
703 s = r"\color{textcolor}" + s
704
705 dpi = self.figure.dpi
706 text_args = []
707 if mtext and (
708 (angle == 0 or
709 mtext.get_rotation_mode() == "anchor") and
710 mtext.get_verticalalignment() != "center_baseline"):
711 # if text anchoring can be supported, get the original coordinates
712 # and add alignment information
713 pos = mtext.get_unitless_position()
714 x, y = mtext.get_transform().transform(pos)
715 halign = {"left": "left", "right": "right", "center": ""}
716 valign = {"top": "top", "bottom": "bottom",
717 "baseline": "base", "center": ""}
718 text_args.extend([
719 f"x={x/dpi:f}in",
720 f"y={y/dpi:f}in",
721 halign[mtext.get_horizontalalignment()],
722 valign[mtext.get_verticalalignment()],
723 ])
724 else:
725 # if not, use the text layout provided by Matplotlib.
726 text_args.append(f"x={x/dpi:f}in, y={y/dpi:f}in, left, base")
727
728 if angle != 0:
729 text_args.append("rotate=%f" % angle)
730
731 _writeln(self.fh, r"\pgftext[%s]{%s}" % (",".join(text_args), s))
732 _writeln(self.fh, r"\end{pgfscope}")
733
734 def get_text_width_height_descent(self, s, prop, ismath):
735 # docstring inherited

Callers 1

draw_texMethod · 0.95

Calls 12

_print_pgf_clipMethod · 0.95
_escape_and_apply_propsFunction · 0.85
_writelnFunction · 0.85
get_rotation_modeMethod · 0.80
get_verticalalignmentMethod · 0.80
get_unitless_positionMethod · 0.80
joinMethod · 0.80
get_alphaMethod · 0.45
get_rgbMethod · 0.45
transformMethod · 0.45
get_transformMethod · 0.45

Tested by

no test coverage detected