Matplotlib 艺术家在放大时保持相同的大小,但也随着平移而移动?

2024-04-08

这是一个非常直接的后续这个问题 https://stackoverflow.com/questions/5678950/matplotlib-artists-to-stay-the-same-size-when-zoomed-in/5679742#comment12497991_5679742.

Using 绘图库,我希望能够在一系列数据标记上放置一种“突出显示栏”,我知道这些数据标记都将位于水平直线上。

这个条形/矩形应该比标记稍高并包含它们,对于下面的三个标记来说是这样的:

为了成为一个明智的突出显示栏,它需要具有以下两个特征:

  • 如果平移绘图,则条形会随标记移动(因此它始终覆盖它们)。
  • 如果缩放图,则条形图的display高度不会改变(因此它总是比标记稍高)。

如果了解一下有帮助的话,这些标记没有有意义的 y 值(它们都在 y=-1 处绘制),只有有意义的 x 值。因此,条形的高度在数据坐标中没有意义;它只需要始终足够高以包围标记即可。


好问题!这是一个很好的挑战,需要结合多种因素才能实现。

首先,我们需要发明一个变换,它将返回预定义值的设备坐标加上基于给定点的偏移量。例如,如果我们知道我们希望条形位于 x_pt、y_pt,那么变换应该表示(以伪代码):

def transform(x, y):
    return x_pt_in_device + x, y_pt_in_device + y

完成此操作后,我们可以使用此变换在固定数据点周围绘制一个 20 像素的框。但是,您只想在 y 方向上绘制固定像素高度的框,但在 x 方向上您想要标准数据缩放。

因此,我们需要创建一个可以独立变换 x 和 y 坐标的混合变换。执行您所要求的操作的整个代码:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.path as mpath
import matplotlib.transforms as mtrans

import numpy as np


class FixedPointOffsetTransform(mtrans.Transform):
    """
    Always returns the same transformed point plus
    the given point in device coordinates as an offset.
    """
    def __init__(self, trans, fixed_point):
        mtrans.Transform.__init__(self)
        self.input_dims = self.output_dims = 2
        self.trans = trans
        self.fixed_point = np.array(fixed_point).reshape(1, 2)

    def transform(self, values):
        fp = self.trans.transform(self.fixed_point)
        values = np.array(values)
        if values.ndim == 1:
            return fp.flatten() + values
        else:
            return fp + values


plt.scatter([3.1, 3.2, 3.4, 5], [2, 2, 2, 6])

ax = plt.gca()
fixed_pt_trans = FixedPointOffsetTransform(ax.transData, (0, 2))

xdata_yfixed = mtrans.blended_transform_factory(ax.transData, fixed_pt_trans)


x = [3.075, 3.425] # x range of box (in data coords)
height = 20 # of box in device coords (pixels)
path = mpath.Path([[x[0], -height], [x[1], -height],
                   [x[1], height],  [x[0], height],
                   [x[0], -height]])
patch = mpatches.PathPatch(path, transform=xdata_yfixed,
                           facecolor='red', edgecolor='black',
                           alpha=0.4, zorder=0)
ax.add_patch(patch)

plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matplotlib 艺术家在放大时保持相同的大小,但也随着平移而移动? 的相关文章

随机推荐