如何加快在 Matplotlib 中绘图的速度?

How to speed up drawing plots in Matplotlib?

我在玩随机游走,我有两点。第一个随机行走,第二个试图逃离他的区域,该区域由公式 e^(-t) 给出,其中 t 是两点之间的距离。在我看来这不是一个困难的程序,但是 运行 它需要大约一分钟才能计算出一百分,所以我请你帮我想办法加快速度并向我解释.

import numpy as np
import matplotlib.pyplot as plt


def f(x, y, zx, zy):
    return np.exp(-np.sqrt((x-zx)**2+(y-zy)**2))


x = y = np.linspace(-100, 100, 1000)
X, Y = np.meshgrid(x, y)

fig, ax = plt.subplots(1, 1, figsize=(12, 6))
#random picking first location for two points
loksx = [np.random.randn(), ]
loksy = [np.random.randn(), ]
lokzx = [np.random.randn(), ]
lokzy = [np.random.randn(), ]
for i in range(100):
    lokzx.append(np.random.randn()+lokzx[-1])
    lokzy.append(np.random.randn()+lokzy[-1])
    nsx = np.random.randn()
    nsy = np.random.randn()
    #checking if the next step has smaller value than the last one 
    if f(loksx[-1]+nsx, loksy[-1]+nsy, lokzx[-1], lokzy[-1]) < f(loksx[-1], loksy[-1], lokzx[-1], lokzy[-1]):
        loksx.append(nsx+loksx[-1])
        loksy.append(nsy+loksy[-1])
Z = []
for i in range(len(lokzx)):
    Z.append(f(X, Y, lokzx[i], lokzy[i]))
ax.plot(lokzx[0], lokzy[0], 'y,',markersize=1)
ax.plot(loksx[0], loksy[0], 'w,', markersize=1)
ax.plot(lokzx[1:-1], lokzy[1:-1], 'g,',markersize=1)
ax.plot(loksx[1:-1], loksy[1:-1], 'b,', markersize=1)

ax.plot(loksx[-1], loksy[-1], 'k,', markersize=1)
ax.plot(lokzx[-1], lokzy[-1], 'r,',markersize=1)
for i in range(len(Z)):
    ax.contourf(X, Y, Z[i], 20, cmap='RdGy', alpha=0.01)
ax.plot()
ax.set_aspect('equal')
plt.show()

@编辑 现在我不使用列表,而是使用 nparrays:

import numpy as np
import matplotlib.pyplot as plt


def f(x, y, zx, zy):
    return np.exp(-np.sqrt((x-zx)**2+(y-zy)**2))


x = y = np.linspace(-100, 100, 1000)
X, Y = np.meshgrid(x, y)

fig, ax = plt.subplots(1, 1, figsize=(12, 6))
lokzx = np.random.randn(100)
lokzy = np.random.randn(100)
loksx = np.zeros(100)
loksy = np.zeros(100)
for i in range(1,100):
    nsx = np.random.randn()
    nsy = np.random.randn()
    if f(loksx[i-1]+nsx, loksy[i-1]+nsy, lokzx[i-1], lokzy[i-1]) < f(loksx[i-1], loksy[i-1], lokzx[i-1], lokzy[i-1]):
        loksx[i] = loksx[i-1]+nsx
        loksy[i] = loksy[i-1]+nsy
    else:
        loksx[i] = loksx[i-1]
        loksy[i] = loksy[i-1]
Z = np.zeros((100,1000,1000))
for i in range(len(lokzx)):
    Z[i] = f(X, Y, lokzx[i], lokzy[i])

ax.plot(lokzx[0], lokzy[0], 'y,',markersize=1)
ax.plot(loksx[0], loksy[0], 'w,', markersize=1)
ax.plot(lokzx[1:-1], lokzy[1:-1], 'g,',markersize=1)
ax.plot(loksx[1:-1], loksy[1:-1], 'b,', markersize=1)

ax.plot(loksx[-1], loksy[-1], 'k,', markersize=1)
ax.plot(lokzx[-1], lokzy[-1], 'r,',markersize=1)
for i in range(len(Z)):
    ax.contourf(X, Y, Z[i], 20, cmap='RdGy', alpha=0.01)
ax.plot()
ax.set_aspect('equal')
plt.show()

但是 运行 这仍然需要一段时间。剩下的唯一问题是这两行代码:

for i in range(len(Z)):
    ax.contourf(X, Y, Z[i], 20, cmap='RdGy', alpha=0.01)

但我不知道如何在不使用循环的情况下重写它。

您应该通过学习使用适当的 numpy 函数来尝试避免循环。即像下面这样的东西会快得多:

dx = np.random.randn(100)
dy = np.random.randn(100)
x = np.cumsum(dx)
y = np.cumsum(dy)

如果您确实需要for-loop,请预分配您的数组;不要继续追加:

x = np.zeros(100)
y = np.zeros(100)
for i in range(1, 100):
    x[i] = x[i-1] + np.random.randn()
    y[i] = y[i-1] + np.random.randn()