3 回答

TA貢獻1878條經(jīng)驗 獲得超4個贊
注釋太長,因此只想確認這LineCollection比在行子段上進行for循環(huán)要快得多。
LineCollection方法在我手中要快得多。
# Setup
x = np.linspace(0,4*np.pi,1000)
y = np.sin(x)
MAP = 'cubehelix'
NPOINTS = len(x)
我們將針對上面的LineCollection方法測試迭代繪圖。
%%timeit -n1 -r1
# Using IPython notebook timing magics
fig = plt.figure()
ax1 = fig.add_subplot(111) # regular resolution color map
cm = plt.get_cmap(MAP)
for i in range(10):
ax1.set_color_cycle([cm(1.*i/(NPOINTS-1)) for i in range(NPOINTS-1)])
for i in range(NPOINTS-1):
plt.plot(x[i:i+2],y[i:i+2])
1 loops, best of 1: 13.4 s per loop
%%timeit -n1 -r1
fig = plt.figure()
ax1 = fig.add_subplot(111) # regular resolution color map
for i in range(10):
colorline(x,y,cmap='cubehelix', linewidth=1)
1 loops, best of 1: 532 ms per loop
如果您想要平滑的漸變并且只有幾個點,那么按照當(dāng)前選擇的答案提供的方法,對線進行向上采樣以獲得更好的顏色漸變?nèi)匀皇且粋€好主意。
添加回答
舉報