Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib中基于坐标的字体大小_Python_Matplotlib - Fatal编程技术网

Python matplotlib中基于坐标的字体大小

Python matplotlib中基于坐标的字体大小,python,matplotlib,Python,Matplotlib,我想在matplotlib中呈现文本,在matplotlib中字体的大小是根据坐标而不是pt来指定的。我的用例是渲染单个字母,其大小与背景中底层框(Patchinstance)的大小匹配 如果我可以独立指定宽度和高度,从而生成缩放字母,那么我的好处就是。根据@ImportanceOfBeingErnest的评论,我得出了以下解决方案: 你能写下你到现在为止都在尝试什么,你在尝试什么样的情节吗?也许是这样?我会试试这个,谢谢。 def set_font_size_in_coord(text, w

我想在matplotlib中呈现文本,在matplotlib中字体的大小是根据坐标而不是pt来指定的。我的用例是渲染单个字母,其大小与背景中底层框(
Patch
instance)的大小匹配


如果我可以独立指定宽度和高度,从而生成缩放字母,那么我的好处就是。

根据@ImportanceOfBeingErnest的评论,我得出了以下解决方案:


你能写下你到现在为止都在尝试什么,你在尝试什么样的情节吗?也许是这样?我会试试这个,谢谢。
def set_font_size_in_coord(text, width=None, height=None, mode="unlocked"):
    from matplotlib.transforms import Bbox
    from matplotlib.text import Text
    from matplotlib.patheffects import AbstractPathEffect

    class TextScaler(AbstractPathEffect):
        def __init__(self, text, width, height, mode):
            self._text = text
            self._mode = mode
            self._width = width
            self._height = height

        def draw_path(self, renderer, gc, tpath, affine, rgbFace=None):
            ax = self._text.axes
            renderer = ax.get_figure().canvas.get_renderer()
            bbox = text.get_window_extent(renderer=renderer)
            bbox = Bbox(ax.transData.inverted().transform(bbox))

            if self._mode == "proportional":
                if self._width is None:
                    # Proportional scaling based on height
                    scale_y = self._height / bbox.height
                    scale_x = scale_y
                elif self._height is None:
                    # Proportional scaling based on width
                    scale_x = self._width / bbox.width
                    scale_y = scale_x
            elif self._mode == "unlocked":
                scale_x = self._width / bbox.width
                scale_y = self._height / bbox.height
            elif self._mode == "minimum":
                scale_x = self._width / bbox.width
                scale_y = self._height / bbox.height
                scale = max(scale_x, scale_y)
                scale_x, scale_y = scale, scale
            elif self._mode == "maximum":
                scale_x = self._width / bbox.width
                scale_y = self._height / bbox.height
                scale = min(scale_x, scale_y)
                scale_x, scale_y = scale, scale

            affine = affine.identity().scale(scale_x, scale_y) + affine
            renderer.draw_path(gc, tpath, affine, rgbFace)

    if mode in ["unlocked", "minimum", "maximum"]:
        if width is None or height is None:
            raise TypeError(
                f"Width and height must be set in '{mode}' mode"
            )
    elif mode == "proportional":
        if  not (width  is None and height is not None) or \
            not (height is None and width  is not None):
                raise TypeError(
                    f"Either width or height must be set in '{mode}' mode"
                )
    else:
        raise ValueError(
                f"Unknown mode '{mode}'"
            )
    text.set_path_effects([TextScaler(text, width, height, mode)])