矩形与fancyboxpatch行为不一致

2024-09-30 03:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到矩形和fancyboxpatch对象的绘制方式不一致。我希望下面的代码生成两个几乎相同的正方形。你知道吗

from matplotlib.patches import Rectangle, FancyBboxPatch
import matplotlib.pyplot as plt

f = plt.figure()
ax = f.add_subplot(1,1,1)

bbox1 = Rectangle((0.4, 0.4), .1, .1,
                  transform=ax.transData, ec='red',fill=False)

bbox2 = FancyBboxPatch((0.4, 0.4), .1, .1, boxstyle='square',
                       transform=ax.transData, ec='green',fill=False)
ax.add_patch(bbox1)
ax.add_patch(bbox2)

f.show()

实际结果与预期不符。在交互式窗口中打印时,平移和缩放的效果与使用transData的对象所预期的一样。似乎FancyBBoxPatch的初始大小和位置并没有像我期望的那样被设置。你能告诉我我错过了什么吗? enter image description here


Tags: 对象importaddfalsematplotlibtransformpltax
1条回答
网友
1楼 · 发布于 2024-09-30 03:25:20

我不知道简单的RTFM的礼节是什么,但是我会贴出答案而不是删除问题。你知道吗

默认情况下,“正方形”样式在框周围的填充为0.3。设置pad=0会产生一致的结果。你知道吗

from matplotlib.patches import Rectangle, FancyBboxPatch
import matplotlib.pyplot as plt

f = plt.figure()
ax = f.add_subplot(1,1,1)

bbox1 = Rectangle((0.4, 0.4), .1, .1,
                  transform=ax.transData, ec='red',fill=False)

bbox2 = FancyBboxPatch((0.4, 0.4), .1, .1, boxstyle='square,pad=0',
                       transform=ax.transData, ec='green',fill=False)
ax.add_patch(bbox1)
ax.add_patch(bbox2)

f.show()

相关问题 更多 >

    热门问题