Python实现流星雨效果的代码

编程知识 更新时间:2023-04-06 00:31:16

绘制一颗流星

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实现流星雨效果的代码

本文发布于:2023-04-06 00:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/d2c59391f9f818b4dca0a199c0578ff8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流星雨   效果   代码   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 47899文章数
  • 14阅读数
  • 0评论数