在图上随机生成点 - matplotlib

时间:2015-09-22 13:27:33

标签: python matplotlib graph

我想在这个情节上随机生成n个点。 我使用过.scatter方法,但这些点似乎不是随机的。有没有一种方法可以简单地传递一个数字,然后产生那个数量的点?

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

def f(t):
    s1 = np.cos(2*np.pi*t)
    e1 = np.exp(-t)
    return np.multiply(s1,e1)


t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
t3 = np.arange(0.0, 2.0, 0.01)

fig = plt.figure(figsize=plt.figaspect(2.))

ax = fig.add_subplot(2, 1, 2, projection='3d')
X = np.arange(-20, 20, 0.25)
xlen = len(X)
Y = np.arange(-20, 20, 0.25)
ylen = len(Y)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color='yellow',
        linewidth=0, antialiased=False)
scatteredpoints = ax.scatter(X[1::20, 1::20],Y[1::20, 1::20],Z[1::20, 1::20],linewidth=0, antialiased=False)

ax.set_zlim3d(-1, 1)

plt.show()

1 个答案:

答案 0 :(得分:2)

您需要将随机数据提供给plt.scatter

def scatterRandomPoints(n):
    plt.scatter(*np.random.randint(100, size = (2, n)))