Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of meteor shower with Python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report