如何在matplotlib中缩放多边形面片?

2024-04-19 19:34:18 发布

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

在下面的示例中,我使用matplotlib.patches.Polygon. 在将补丁添加到绘图之前,有没有办法缩放补丁?在

我试过用matplotlib.transforms.Affine2D以各种各样的方式都没有成功。与往常一样,关于转换的matplotlib文档是远远不够的。在

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])

x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
poly = Polygon( zip(x,y), facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)

plt.show()

Tags: importadd绘图示例matplotlibfigpltred
1条回答
网友
1楼 · 发布于 2024-04-19 19:34:18

如果你所说的比例是乘以一个因子,你可以很容易地通过numpy来实现。在

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])

x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
scale = 2
poly = Polygon( np.c_[x,y]*scale, facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)

plt.show()

同样可以通过matplotlib.transforms.Affine2D()转换实现。在

^{pr2}$

尽管对于这样简单的缩放来说,这似乎有点过头了。在

相关问题 更多 >