python - matplotlib:创建大量 Patch 对象的有效方法

标签 python numpy matplotlib

我有一些 Python 代码,使用来自某些海洋模型数据的 matplotlib 绘制大量非常规多边形。

我通过创建 4 个形状为 (N,2) 的 numpy 数组来定义每个补丁的角,其中 N 是一个很大的数字(比如 500,000)

然后我为每组角创建 matplotlib Patch 对象并将其添加到列表中。最后,我从补丁列表中创建了一个 matplotlib PatchCollection 对象。

问题在于 Patch 的生成速度很慢,因为它处于 for 循环中。我一直在想办法用 numpy 广播来加快速度,但无法完全破解它。

这里有一些示例代码,带有一个小的测试数据集(显然运行起来很快)。

import numpy as np
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt


# Cell lat/lon centers:
lons = np.array([ 323.811,  323.854,  323.811,  323.723, 324.162,  324.206,  323.723,  324.162, 323.635,  323.679])
lats = np.array([-54.887, -54.887, -54.858, -54.829, -54.829, -54.829, -54.799, -54.799, -54.770, -54.770])

# Cell size scaling factors:
cx = np.array([1,1,1,2,2,2,4,1,2,1]) 
cy = np.array([1,1,1,1,2,2,2,1,2,1])

# Smallest cell sizes:
min_dlon = 0.0439453  
min_dlat = 0.0292969 

# Calculate cell sizes based on cell scaling factor and smallest cell size
dlon = cx * min_dlon
dlat = cy * min_dlat

# calculate cell extnets....
x1 = lons - 0.5 * dlon
x2 = lons + 0.5 * dlon
y1 = lats - 0.5 * dlat
y2 = lats + 0.5 * dlat

# ... and corners
c1 = np.array([x1,y1]).T
c2 = np.array([x2,y1]).T
c3 = np.array([x2,y2]).T
c4 = np.array([x1,y2]).T

# Now loop over cells and create Patch objects from the cell corners.
# This is the bottleneck as it using a slow Python loop instead of 
# fast numpy broadcasting. How can I speed this up?
ncel = np.alen(lons)
patches = []
for i in np.arange(ncel):
    verts = np.vstack([c1[i], c2[i], c3[i], c4[i]])
    p = plt.Polygon(verts)
    patches.append(p)

# Create patch collection from list of Patches
p = PatchCollection(patches, match_original=True)

有什么方法可以加快速度吗?

最佳答案

如何通过 matplolib.collections 创建集合而不是创建每个多边形(或面片)? 看看这里的例子:http://matplotlib.org/examples/api/collections_demo.html

并阅读 matplotlib 文档:http://matplotlib.org/api/collections_api.html?highlight=polycollection#matplotlib.collections.PolyCollection

此示例代码在 ~10 秒内添加 200,000 个多边形:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
import matplotlib

npol, nvrts = 200000, 5
cnts = 100 * (np.random.random((npol,2)) - 0.5)
offs = 10 * (np.random.random((nvrts,npol,2)) - 0.5)
vrts = cnts + offs
vrts = np.swapaxes(vrts, 0, 1)
z = np.random.random(npol) * 500

fig, ax = plt.subplots()
coll = PolyCollection(vrts, array=z, cmap=matplotlib.cm.jet)
ax.add_collection(coll)
ax.autoscale()
plt.show()

enter image description here

关于python - matplotlib:创建大量 Patch 对象的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37573314/

相关文章:

python - 绘制 sum(matrix,axis=0) 并用求和所需的非零元素的行索引标记该图

numpy - 创建一个绿色 channel 比蓝色和红色 channel 更亮的蒙版?

python - Matplotlib:如何为数据分配正确的 y 轴刻度?

python - pyqt中的多个嵌入式matplotlib Canvas 在鼠标悬停时改变大小

python - 立方根是整数吗

python - 将元组继承为列表

python - Pandas 模块运行时错误

python - 发送消息时禁用登录 Flask-Mail

Python cx_Freeze __init__ "no module named codecs"

python - 在 Matplotlib/Python 中更改极坐标图的轴选项