gpt4 book ai didi

python - matplotlib 中的固定大小矩形?

转载 作者:行者123 更新时间:2023-12-01 08:51:03 24 4
gpt4 key购买 nike

有没有办法绘制一个跨越 X 轴整个长度的矩形 ,但具有固定的静态高度,例如 20 像素? 矩形的高度应保持为常数无论缩放或调整绘图大小,像素的数量。我花了几个小时寻找解决方案,但我就是无法使其发挥作用。有什么建议么?谢谢enter image description here

最佳答案

您想要创建一个矩形,在轴坐标中定位,并在轴坐标中水平调整大小,但高度以像素(屏幕)坐标为单位。

这里棘手的一点是,简单地应用混合变换是行不通的,因为 y 位置需要位于与矩形高度不同的坐标系中;在 y 方向上没有问题,因为位置和宽度都是相同的坐标系。
下面显示了三种可能的选项。

A.使用偏移框

解决方案是创建矩形并将其打包到 matplotlib.offsetbox.AuxTransformBox 中。然后将混合变换应用于 AuxTransformBox 将仅影响宽度和高度。
然后可以将 AuxTransformBox 打包到 matplotlib.offsetbox.AnchoredOffsetbox 中。 。与图例类似,它位于 bbox 内,默认情况下是轴 bbox。由于轴确实是此处使用的所需系统,因此无需指定 bbox_to_anchor 。在坐标区 bbox 内部,选择左下角作为 anchor (loc="lower left")。

import matplotlib.pyplot as plt
import matplotlib.offsetbox
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()

# create rectangle with
# * lower left corner at (0,0); will later be interpreted as axes coordinates
# * width=1; will later be interpreted in axes coordinates
# * height=20; will later be interpreted as pixel coordinates
rect = plt.Rectangle((0,0), 1,20)

# create transform; axes coordinates along x axis, pixel coordinates along y axis
trans = mtransforms.blended_transform_factory(ax.transAxes,
mtransforms.IdentityTransform())
# create an offset box from the above transform; the contents will be transformed
# with trans from above
aux = matplotlib.offsetbox.AuxTransformBox(trans)
aux.add_artist(rect)

# create an anchored offsetbox. Its child is the aux box from above,
# its position is the lower left corner of the axes (loc="lower left")
ab = matplotlib.offsetbox.AnchoredOffsetbox("lower left", pad=0, borderpad=0, frameon=False)
ab.set_child(aux)

ax.add_artist(ab)
plt.show()

enter image description here

现在,即使在平移/缩放/重新缩放时,矩形也始终保持与轴相连。

B.使用回调

或者,您可以使用回调来调整矩形的高度。

我。轴坐标,更新高度

这里可以在轴坐标中定义矩形,这对于位置和宽度都很有用。然后,高度可以计算为 20 像素除以轴的高度(以像素为单位)。然后,您可以在每次调整图形大小并且轴的高度发生变化时重新计算高度。

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()

height = 20 # pixels
rect = plt.Rectangle((0,0), 1,1, transform=ax.transAxes)
ax.add_patch(rect)

def update_rect(evt=None):
bbox_pixel = mtransforms.TransformedBbox(ax.get_position(), fig.transFigure)
rect.set_height(height/bbox_pixel.height)

update_rect()
fig.canvas.mpl_connect("resize_event", update_rect)

plt.show()

ii.像素坐标,更新位置和宽度

同样,您当然可以在像素坐标中定义矩形,并使用回调根据实际轴大小设置宽度和位置。

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()

pos = (0,0) #axes coordinates
width = 1 # -"-
rect = plt.Rectangle((0,0), 20,20, transform=None)
ax.add_patch(rect)

def update_rect(evt=None):
bbox_pixel = mtransforms.TransformedBbox(ax.get_position(), fig.transFigure)
print(bbox_pixel.width)
rect.set_width(bbox_pixel.width*width)
rect.set_xy((bbox_pixel.x0 + pos[0]*bbox_pixel.width,
bbox_pixel.y0 + pos[1]*bbox_pixel.height))

update_rect()
fig.canvas.mpl_connect("resize_event", update_rect)

plt.show()

C.创建插入轴

您还可以创建一个位于左下角的插入轴,其宽度为父轴的 100%,高度为 20 像素/图形 dpi。在该插图内,您可以创建一个填充完整轴的矩形。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

fig, ax = plt.subplots()

rect_ax = inset_axes(ax, "100%", 20/fig.dpi, loc="lower left", borderpad=0)
rect_ax.axis("off")

rect=plt.Rectangle((0,0), 1,1, transform=rect_ax.transAxes)
rect_ax.add_patch(rect)

plt.show()

关于python - matplotlib 中的固定大小矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com