【问题标题】:Plotting patches of random shapes with matplotlib使用 matplotlib 绘制随机形状的补丁
【发布时间】:2016-03-12 14:01:30
【问题描述】:

这是我要绘制的目标:
几个不规则形状的椭圆。


(来源:clouddn.com

我正在考虑生成一些随机数作为顶点位置。

但它只能构建一个多边形。那么,如何绘制多条弧线并使其闭合呢?

【问题讨论】:

  • jpg图片的链接不存在了

标签: python matplotlib patch


【解决方案1】:

要创建任意形状,您需要使用 matplotlib.patches.Polygon 类并提供足够的 x,y 样本以使其看起来像必要的平滑路径(在一天结束时它仍然是直线段当你放大到足够近时)。

如果您只有几个点,您可以使用多个interpolation methods 之一(例如scipy.interpolate.spline)来创建数据的平滑插值,然后您可以将其提供给Polygon 构造函数。

这是一个使用Polygon 类通过在圆周围提供x,y 点来创建圆的简单示例。

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

# Circle coordinates (100 points around the circle)
t = np.linspace(0, 2 * np.pi, 100).reshape(100,1)
coords = np.concatenate((np.cos(t), np.sin(t)), axis=1)

ax = plt.axes()

polygons = [];
polygons.append(Polygon(coords))
p = PatchCollection(polygons, alpha=0.4)
ax.add_collection(p)
ax.axis('equal')

【讨论】:

    【解决方案2】:

    听起来就像官方文档中的例子:

    http://matplotlib.org/examples/pylab_examples/ellipse_demo.html

    主要部分,他们只是构造一个椭圆列表:

    ells = [Ellipse(xy=rnd.rand(2)*10, width=rnd.rand(), height=rnd.rand(), angle=rnd.rand()*360) for i in range(250)]
    

    ...还是我错过了你的意思? :)

    【讨论】:

    • 原始问题指出它们不是常规省略号(您也可以在图中看到)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多