Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of meteor shower with Python

Shulou Source: shulou.com Published: 2022-06-02 05:57:03 09月18日 Update

This article will explain in detail how to use Python to achieve meteor shower effect. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The premise of a meteor shower is that there must be a meteor first. The so-called meteor is a straight line dragging its tail. The so-called dragging tail is actually our romantic imagination, and the essence is nothing more than a gradual change in size and color.

However, matplotlib can not map multiple colors to a curve, so it can split a line into multiple strips, as shown in the figure.

The code is

Import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionx0,y0 = 1 segments 1 # this is the meteor position ts = np.arange) # Parameter xs,ys = x0+ts, y0+ts # drawing line points = np.array ([xs,ys]). T.reshape (- 1) 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 ()

The next step is to turn this meteor into a meteor shower, it's very simple, just add more meteors, the effect is as follows, although it's in the daytime, it doesn't feel interesting.

The code is

From numpy.random import rand, randintN,L = 20100 # number of meteors and number of line segments ts = np.array ([np.linspace (0Magna rand (), L) for _ in range (N)]) .Tx0, y0 = rand (2cm N). Reshape (2m 1m N) x 0 * = 5xsjinys = x0+ts, y0+ts # drawing lines 1points = np.array ([xs,ys]) .T.reshape (NJL) -1 ax 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 [::-1jue I]) ax.add_collection (lc) ax.set_xlim (0,6) ax.set_ylim (- 2) 3) ax.set_axis_off () # cancel axis plt.show ()

If you want to make it dark, the way is

Plt.figure (facecolor= "black") # sets the background to black

In addition, after the background is set to black, pay attention to adjust the color mapping, want to know more specific color mapping table stamp here: pseudo-color mapping in matplotlib

Lc = LineCollection (segs, cmap='viridis_r')

So that's it.

This is the end of the article on "how to use Python to achieve the effect of meteor shower". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Meteor effect meteor shower color more article code that is tail line background black drawing good practical romantic next number location content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Microsoft MySQL Shulou Information