admin管理员组文章数量:1794759
Python实现流星雨效果的代码
绘制一颗流星
import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection x0,y0 = 1,1 #此为流星位置 ts = np.arange(0,1,0.01) #参数 xs,ys = x0+ts, y0+ts #绘图线条 points = np.array([xs, ys]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) ax = plt.subplot() lc = LineCollection(segments, cmap='viridis') lc.set_array(ts) lc.set_linewidth(ts[::-1]) line = ax.add_collection(lc) ax.set_xlim(0, 3) ax.set_ylim(0, 3) plt.show()很多流星
from numpy.random import rand, randint from matplotlib.collections import LineCollection import numpy as np import matplotlib.pyplot as plt N, L = 20, 100 # 流星个数和线段数 ts = np.array([ np.linspace(0, rand(), L) for _ in range(N)]).T x0, y0 = rand(2 * N).reshape(2, 1, N) x0 *= 5 xs, ys = x0 + ts, y0 + ts # 绘图线条1 points = np.array([xs, ys]).T.reshape(N, L, -1, 2) ax = plt.subplot() for i in range(N): segs = np.concatenate([points[i][:-1], points[i][1:]], axis=1) lc = LineCollection(segs, cmap='viridis') lc.set_array(ts[:, i]) lc.set_linewidth(ts[::-1, i]) ax.add_collection(lc) ax.set_xlim(0, 6) ax.set_ylim(-2, 3) ax.set_axis_off() # 取消坐标轴 plt.show()
版权声明:本文标题:Python实现流星雨效果的代码 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686987310a125784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论