![](/img/trans.png)
[英]How to trace the path of a patches.Rectangle object in matplotlib animation?
[英]Avoid slow looping when plotting irregular raster plot using patches.Rectangle
我已經編寫了一個代碼以繪制不規則的柵格圖(即柵格矩形大小可變的柵格圖)。 這是下面的最小可重現示例。
問題在於,在我的示例中,在塊上循環非常慢(每個圖都有很多矩形,並且要繪制很多圖)。 我試圖將坐標轉換為元組列表,但這引發了錯誤。
是否可以獲取patch.Rectangle返回補丁列表,而不是一個列表,所以我可以擺脫補丁循環並加快代碼運行速度?
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import matplotlib.colorbar as cbar
fig,ax=plt.subplots(1)
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
N = 30
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
colors=np.random.rand(N)
normal = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
c=cmap(colors)
for i in range(N):
val=0.5
rect=patches.Rectangle((x[i],y[i]),s[i],s[i],
edgecolor='black',
linewidth = 1,
facecolor = c[i],
)
ax.add_patch(rect)
cax, _ = cbar.make_axes(ax)
cb2 = cbar.ColorbarBase(cax, cmap=cmap,norm=normal)
plt.savefig("test.png")
輸出:
我不知道確切的時間,但是對於PatchCollection
來說,那似乎是完美的工作。 這會完全加快您的圖表速度嗎?
編輯:粗略的測試似乎表明PatchCollection
可以提高性能,特別是當N大時。 我在這里用N = 1000進行了測試:
%timeit withCollection()
316 ms ± 5.41 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit withoutCollection()
772 ms ± 30.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
完整代碼:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import matplotlib.colorbar as cbar
from matplotlib.collections import PatchCollection
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
N = 30
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
colors=np.random.rand(N)
normal = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
c=cmap(colors)
pat = []
for i in range(N):
rect=patches.Rectangle((x[i],y[i]),s[i],s[i])
pat.append(rect)
col = PatchCollection(pat)
col.set_facecolor(c)
col.set_edgecolor('k')
col.set_linewidth(1.)
ax.add_collection(col)
cax, _ = cbar.make_axes(ax)
cb2 = cbar.ColorbarBase(cax, cmap=cmap,norm=normal)
一句話摘要:使用PolyCollection
。
使用集合繪制許多形狀肯定比繪制單個矩形更有效。 另一個答案建議使用PatchCollection
。 使用PolyCollection
更有效。
原因是雙重的:
我對有關顏色定義(最好讓該集合為您完成)和顏色條(使用該集合,而不是獨立的顏色條)的代碼進行了一些修改
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from matplotlib.collections import PatchCollection, PolyCollection
import matplotlib.transforms as mtrans
PatchCollection:
def patchcoll(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng+1)
plt.xlim(0,rng+1)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
pat = []
for i in range(N):
rect=patches.Rectangle((x[i],y[i]),s[i],s[i])
pat.append(rect)
col = PatchCollection(pat, cmap=cmap, norm=norm)
col.set_array(c)
col.set_edgecolor('k')
col.set_linewidth(1.)
ax.add_collection(col)
fig.colorbar(col)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
PolyCollection:
def polycoll(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
offsets = np.c_[x,y]
verts = list(zip([0,1,1,0,0], [0,0,1,1,0]))
col = PolyCollection([verts], sizes=s, offsets=offsets,
transOffset=mtrans.IdentityTransform(),
offset_position="data", cmap=cmap, norm=norm)
col.set_array(c)
col.set_edgecolor('k')
col.set_linewidth(1.)
ax.add_collection(col)
fig.colorbar(col)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
單矩形:
def rectangles(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
for i in range(N):
rect=patches.Rectangle((x[i],y[i]),s[i],s[i],
facecolor=cmap(norm(c[i])), edgecolor="k", linewidth=1)
ax.add_patch(rect)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
fig.colorbar(sm)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
運行所有:
patchcoll(30, show=True)
polycoll(30,show=True)
rectangles(30,show=True)
對於N=1000
我得到
%timeit(rectangles(1000))
757 ms ± 4.26 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit(patchcoll(1000))
184 ms ± 462 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit(polycoll(1000))
58.3 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
因此,在這種情況下,使用PatchCollection
比單個矩形的效率高3倍,而使用PolyCollection
比PatchCollection
效率高3 PatchCollection
。
使用上面的3種不同方法創建具有N
矩形的圖形所需的時間概述:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.