gpt4 book ai didi

python - 使用 matplotlib 更有效地绘制多边形

转载 作者:IT老高 更新时间:2023-10-28 20:32:54 24 4
gpt4 key购买 nike

我有大约 60000 个形状的日期集(每个角的纬度/经度坐标),我想使用 matplotlib 和 basemap 在 map 上绘制。

这就是我目前的做法:

for ii in range(len(data)):
lons = np.array([data['lon1'][ii],data['lon3'][ii],data['lon4'][ii],data['lon2'][ii]],'f2')
lats = np.array([data['lat1'][ii],data['lat3'][ii],data['lat4'][ii],data['lat2'][ii]],'f2')
x,y = m(lons,lats)
poly = Polygon(zip(x,y),facecolor=colorval[ii],edgecolor='none')
plt.gca().add_patch(poly)

但是,这在我的机器上大约需要 1.5 分钟,我正在考虑是否可以加快速度。有没有更有效的方法来绘制多边形并将它们添加到 map 中?

最佳答案

您可以考虑创建多边形集合而不是单个多边形。

相关文档可以在这里找到:http://matplotlib.org/api/collections_api.html这里有一个值得选择的例子:http://matplotlib.org/examples/api/collections_demo.html

举个例子:

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

# Generate data. In this case, we'll make a bunch of center-points and generate
# verticies by subtracting random offsets from those center-points
numpoly, numverts = 100, 4
centers = 100 * (np.random.random((numpoly,2)) - 0.5)
offsets = 10 * (np.random.random((numverts,numpoly,2)) - 0.5)
verts = centers + offsets
verts = np.swapaxes(verts, 0, 1)

# In your case, "verts" might be something like:
# verts = zip(zip(lon1, lat1), zip(lon2, lat2), ...)
# If "data" in your case is a numpy array, there are cleaner ways to reorder
# things to suit.

# Color scalar...
# If you have rgb values in your "colorval" array, you could just pass them
# in as "facecolors=colorval" when you create the PolyCollection
z = np.random.random(numpoly) * 500

fig, ax = plt.subplots()

# Make the collection and add it to the plot.
coll = PolyCollection(verts, array=z, cmap=mpl.cm.jet, edgecolors='none')
ax.add_collection(coll)
ax.autoscale_view()

# Add a colorbar for the PolyCollection
fig.colorbar(coll, ax=ax)
plt.show()

enter image description here

HTH,

关于python - 使用 matplotlib 更有效地绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12881848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com