本文整理汇总了Python中matplotlib.mathtext.MathTextParser.to_rgba方法的典型用法代码示例。如果您正苦于以下问题:Python MathTextParser.to_rgba方法的具体用法?Python MathTextParser.to_rgba怎么用?Python MathTextParser.to_rgba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.mathtext.MathTextParser
的用法示例。
在下文中一共展示了MathTextParser.to_rgba方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RendererH5Canvas
# 需要导入模块: from matplotlib.mathtext import MathTextParser [as 别名]
# 或者: from matplotlib.mathtext.MathTextParser import to_rgba [as 别名]
#.........这里部分代码省略.........
if clippath is not None:
self._path_to_h5(self.ctx,clippath, clippath_trans, stroke=False)
self.ctx.save()
self.ctx.clip()
(x,y) = self.flip.transform((x,y))
im.flipud_out()
rows, cols, im_buffer = im.as_rgba_str()
self._slipstream_png(x, (y-h), im_buffer, cols, rows)
if clippath is not None:
self.ctx.restore()
def _get_font(self, prop):
key = hash(prop)
font = self.fontd.get(key)
if font is None:
fname = findfont(prop)
font = self.fontd.get(fname)
if font is None:
font = FT2Font(str(fname))
self.fontd[fname] = font
self.fontd[key] = font
font.clear()
font.set_size(prop.get_size_in_points(), self.dpi)
return font
def draw_tex(self, gc, x, y, s, prop, angle, ismath=False):
logger.error("Tex support is currently not implemented. Text element '%s' will not be displayed..." % s)
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
if self._last_clip is not None or self._last_clip_path is not None: self._reset_clip()
t = time.time()
if ismath:
self._draw_mathtext(gc, x, y, s, prop, angle)
return
angle = math.radians(angle)
width, height, descent = self.get_text_width_height_descent(s, prop, ismath)
x -= math.sin(angle) * descent
y -= math.cos(angle) * descent
ctx = self.ctx
if angle != 0:
ctx.save()
ctx.translate(x, y)
ctx.rotate(-angle)
ctx.translate(-x, -y)
font_size = self.points_to_pixels(prop.get_size_in_points())
font_str = '%s %s %.3gpx %s, %s' % (prop.get_style(), prop.get_weight(), font_size, prop.get_name(), prop.get_family()[0])
ctx.font = font_str
# Set the text color, draw the text and reset the color to black afterwards
ctx.fillStyle = mpl_to_css_color(gc.get_rgb(), gc.get_alpha())
ctx.fillText(unicode(s), x, y)
ctx.fillStyle = '#000000'
if angle != 0:
ctx.restore()
self._text_time = time.time() - t
def _draw_mathtext(self, gc, x, y, s, prop, angle):
"""Draw math text using matplotlib.mathtext."""
# Render math string as an image at the configured DPI, and get the image dimensions and baseline depth
rgba, descent = self.mathtext_parser.to_rgba(s, color=gc.get_rgb(), dpi=self.dpi, fontsize=prop.get_size_in_points())
height, width, tmp = rgba.shape
angle = math.radians(angle)
# Shift x, y (top left corner) to the nearest CSS pixel edge, to prevent resampling and consequent image blurring
x = math.floor(x + 0.5)
y = math.floor(y + 1.5)
ctx = self.ctx
if angle != 0:
ctx.save()
ctx.translate(x, y)
ctx.rotate(-angle)
ctx.translate(-x, -y)
# Insert math text image into stream, and adjust x, y reference point to be at top left of image
self._slipstream_png(x, y - height, rgba.tostring(), width, height)
if angle != 0:
ctx.restore()
def flipy(self):
return True
def get_canvas_width_height(self):
return self.width, self.height
def get_text_width_height_descent(self, s, prop, ismath):
if ismath:
image, d = self.mathtext_parser.parse(s, self.dpi, prop)
w, h = image.get_width(), image.get_height()
else:
font = self._get_font(prop)
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
w, h = font.get_width_height()
w /= 64.0 # convert from subpixels
h /= 64.0
d = font.get_descent() / 64.0
return w, h, d
def new_gc(self):
return GraphicsContextH5Canvas()
def points_to_pixels(self, points):
# The standard desktop-publishing (Postscript) point is 1/72 of an inch
return points/72.0 * self.dpi