我的代码可以很好地生成和绘制随机游走。但是,我想根据跳跃的大小为每条线着色。这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
import random
def randomwalk(N):
x, y, bigness = np.zeros((N)), np.zeros((N)), np.zeros((N))
for n in range(0,N):
angle = random.random() * 2*np.pi
jump = np.random.normal(0, 50)
x[n] = x[n-1] + (np.cos(angle) * jump)
y[n] = y[n-1] + (np.sin(angle) * jump)
bigness[n] = abs(jump)
return x, y, bigness
x, y, bigness = randomwalk(100)
plt.plot(x, y)
plt.show()
现在,如果我将倒数第二行更改为
plt.scatter(x, y, c=bigness)
然后我得到一堆具有所需颜色的点,但没有连接它们的线。相反,“绘图”功能没有单独着色的选项。
我想要来自“绘图”功能的线条,但来自“分散”功能的着色。我该怎么做呢?