【问题标题】:matplotlib - radius in polygon edges - is it possible?matplotlib - 多边形边缘的半径 - 有可能吗?
【发布时间】:2013-10-09 11:22:25
【问题描述】:

我正在 matplotlib 中绘制一个多边形。我输入了所有点的坐标。在某些点之间,我希望有“圆形”或“径向”边缘而不是直线(比如绘图上的第 1 点和第 2 点。这可能吗?如果不是,那么绘制它的最有效方法是什么?

编辑: Rutger 的解决方案效果很好。

【问题讨论】:

  • 您的意思是要拟合平滑曲线?
  • 我的意思是一个具有两个指定点和半径的拱门。

标签: python matplotlib polygon


【解决方案1】:

您可以通过从路径制作多边形来使用弧线。

一个普通的正方形:

import matplotlib.path as mpath
import matplotlib.patches as patches

verts = [(0,0),
         (1,0),
         (1,1),
         (0,1),
         (0,0)]

codes = [mpath.Path.MOVETO] + (len(verts)-1)*[mpath.Path.LINETO]
square_verts = mpath.Path(verts, codes)

fig, ax = plt.subplots(subplot_kw={'aspect': 1.0, 'xlim': [-0.2,1.2], 'ylim': [-0.2,1.2]})

square = patches.PathPatch(square_verts, facecolor='orange', lw=2)
ax.add_patch(square)

圆角正方形可以用:

verts = [(0.2, 0.0),
         (0.8, 0.0), # start of the lower right corner
         (1.0, 0.0), # intermediate point (as if it wasn't rounded)
         (1.0, 0.2), # end point of the lower right corner
         (1.0, 0.8), # move to the next point etc.
         (1.0, 1.0),
         (0.8, 1.0),
         (0.2, 1.0),
         (0.0, 1.0),
         (0.0, 0.8),
         (0.0, 0.2),
         (0.0, 0.0),
         (0.2, 0.0)]

codes = [mpath.Path.MOVETO,
         mpath.Path.LINETO,
         mpath.Path.CURVE3,
         mpath.Path.CURVE3,
         mpath.Path.LINETO,
         mpath.Path.CURVE3,
         mpath.Path.CURVE3,
         mpath.Path.LINETO,
         mpath.Path.CURVE3,
         mpath.Path.CURVE3,
         mpath.Path.LINETO,
         mpath.Path.CURVE3,
         mpath.Path.CURVE3]


rounded_verts = mpath.Path(verts, codes)

fig, ax = plt.subplots(subplot_kw={'aspect': 1.0, 'xlim': [-0.2,1.2], 'ylim': [-0.2,1.2]})

rounded_verts = patches.PathPatch(rounded_verts, facecolor='orange', lw=2)
ax.add_patch(rounded_verts)

对于您的示例,您需要指定一个中间点,它使用来自 Point1 的 x-coordinate 和来自 Point2 的 y-coordinate

matplotlib 路径教程提供了如何制作路径的详细说明: http://matplotlib.org/users/path_tutorial.html

【讨论】:

  • 第一个代码清单中的变量“x”应该是“verts”。
  • 一个很好解释的答案。欣赏它,Rutger
【解决方案2】:

此类适用于一般多边形。 只需指定顶点和角的填充/半径。

import numpy as np
from matplotlib import patches, path, pyplot as plt


class RoundedPolygon(patches.PathPatch):
    def __init__(self, xy, pad, **kwargs):
        p = path.Path(*self.__round(xy=xy, pad=pad))
        super().__init__(path=p, **kwargs)

    def __round(self, xy, pad):
        n = len(xy)

        for i in range(0, n):

            x0, x1, x2 = np.atleast_1d(xy[i - 1], xy[i], xy[(i + 1) % n])

            d01, d12 = x1 - x0, x2 - x1
            d01, d12 = d01 / np.linalg.norm(d01), d12 / np.linalg.norm(d12)

            x00 = x0 + pad * d01
            x01 = x1 - pad * d01
            x10 = x1 + pad * d12
            x11 = x2 - pad * d12

            if i == 0:
                verts = [x00, x01, x1, x10]
            else:
                verts += [x01, x1, x10]
        codes = [path.Path.MOVETO] + n*[path.Path.LINETO, path.Path.CURVE3, path.Path.CURVE3]

        return np.atleast_1d(verts, codes)


# Test
xy = np.array([(0, 0), (0.25, 0), (0.5, -0.25), (0.75, 0),
               (1, 0), (1, 0.25), (1.25, 0.5), (1, 0.75),
               (1, 1), (0.75, 1), (0.5, 1.25), (0.25, 1),
               (0, 1), (0, 0.75), (-0.25, 0.5), (0, 0.25)])
rp = RoundedPolygon(xy=xy, pad=0.1, facecolor='red', edgecolor='magenta', lw=3)

fig, ax = plt.subplots()
ax.add_patch(rp)

ax.set_aspect(1)
ax.axis('off')
ax.set_xlim(-1, 2)
ax.set_ylim(-1, 2)
plt.savefig('star.png')

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 2016-11-21
    • 2021-10-01
    • 1970-01-01
    • 2013-04-29
    • 2018-03-07
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多