26

matplotlibとbasemapを使用して地図上に描画したい約60000の形状(各コーナーの緯度/経度座標)の日付セットがあります。

これは私が現在それをしている方法です:

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分くらいかかるので、少しスピードアップできるかと思っていました。ポリゴンを描画してマップに追加するためのより効率的な方法はありますか?

4

2 に答える 2

38

個々のポリゴンの代わりに、ポリゴンのコレクションを作成することを検討できます。

関連するドキュメントはここにあります: http: //matplotlib.org/api/collections_api.html ここでappartを選ぶ価値のある例: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()

ここに画像の説明を入力してください

HTH、

于 2012-10-14T15:32:29.253 に答える
4

私は自分のコードを調整しました、そして今それは完璧に働いています:)

実例は次のとおりです。

lons = np.array([data['lon1'],data['lon3'],data['lon4'],data['lon2']])
lats = np.array([data['lat1'],data['lat3'],data['lat4'],data['lat2']])
x,y = m(lons,lats)
pols = zip(x,y)
pols = np.swapaxes(pols,0,2)
pols = np.swapaxes(pols,1,2)
coll = PolyCollection(pols,facecolor=colorval,cmap=jet,edgecolor='none',zorder=2)
plt.gca().add_collection(coll)
于 2012-10-16T12:59:28.000 に答える