Wednesday, 2 April 2025

Velvet Nebula Pattern using Python


import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

theta=np.linspace(0,4*np.pi,400)

z=np.linspace(-2,2,400)

r=z**2+1

x=r*np.sin(theta)

y=r*np.cos(theta)

fig=plt.figure(figsize=(10,8))

ax=fig.add_subplot(111,projection='3d')

colors=np.linspace(0,1,len(x))

ax.scatter(x,y,z,c=colors,cmap='plasma',s=10)

ax.set_xlabel('X Axis')

ax.set_ylabel('Y Axis')

ax.set_zlabel('Z Axis')

ax.set_title('Velvet Nebula Bloom')

plt.show()

#source code --> clcoding.com

 Code Explanation:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

 

import numpy as np:

Imports the NumPy library using the alias np.

NumPy is used for efficient numerical calculations and generating arrays of data points.

 

import matplotlib.pyplot as plt:

Imports Matplotlib’s pyplot module using the alias plt.

It provides functions to plot graphs, charts, and visualizations.

 

from mpl_toolkits.mplot3d import Axes3D:

Imports Axes3D from the mpl_toolkits.mplot3d module.

It allows creating 3D plots using Matplotlib.

 

2. Generate Data for the Spiral

a) Theta - Angle Generation

theta = np.linspace(0, 4 * np.pi, 400)

np.linspace(start, stop, num):

Generates 400 evenly spaced values from 0 to 4π (approx 12.566).

theta represents the angle in radians for a circular or spiral motion.

This variable will be used to define the x and y coordinates using sine and cosine functions.

 

b) Z - Vertical Axis (Height)

z = np.linspace(-2, 2, 400)

Generates 400 points from -2 to 2, representing the Z-axis (height).

This makes the spiral extend vertically from -2 to 2 units.

 

c) R - Spiral Radius

r = z**2 + 1

This creates a varying radius using the formula:

𝑟=𝑧2+1

 

Explanation:

At the center (z = 0), the radius is 1.

As z increases or decreases, the radius expands because  increases.

This makes the spiral bloom outward, resembling a nebula.

 

d) Calculate X and Y Coordinates

x = r * np.sin(theta)

y = r * np.cos(theta)

x = r × sin(θ) and y = r × cos(θ):

 

These parametric equations define the position of points in a circular (spiral) pattern.

sin() and cos() determine the coordinates on the XY-plane.

r controls how far the points are from the origin, forming a widening spiral.

 

3. Create the Plot

fig = plt.figure(figsize=(12, 8))

ax = fig.add_subplot(111, projection='3d')

plt.figure(figsize=(12, 8)):

Creates a figure with a size of 12x8 inches for better visualization.

fig.add_subplot(111, projection='3d'):

Adds a 3D subplot to the figure.

111 means 1 row, 1 column, and this is the first subplot.

projection='3d' enables 3D plotting.

 

4. Apply a Color Gradient

colors = np.linspace(0, 1, len(x))

np.linspace(0, 1, len(x)):

Generates an array of values from 0 to 1 to map colors.

len(x) ensures there is one color for each point.

These values will be used to create a smooth gradient using a colormap.

 

Plot the Spiral Using Scatter Plot

ax.scatter(x, y, z, c=colors, cmap='plasma', s=10)

ax.scatter():

 

Creates a 3D scatter plot.

Each point is represented as a small dot in 3D space.

Parameters:

x, y, z: Coordinates for each point.

c=colors: The color values for each point (gradient).

cmap='plasma': Uses the Plasma colormap, giving a vibrant velvet glow.

s=10: Sets the size of the points to 10 pixels.

 

5. Add Axis Labels and Title

ax.set_xlabel('X Axis')

ax.set_ylabel('Y Axis')

ax.set_zlabel('Z Axis')

ax.set_title('Velvet Nebula Bloom: 3D Spiral Pattern using Matplotlib')

ax.set_xlabel(): Labels the X-axis.

ax.set_ylabel(): Labels the Y-axis.

ax.set_zlabel(): Labels the Z-axis.

ax.set_title(): Sets the plot title, describing the visualization as Velvet Nebula Bloom.

 

6. Display the Plot

plt.show()

plt.show():

Renders the plot and displays it in a window.

You can rotate, zoom, and explore the plot using the interactive Matplotlib interface.


Related Posts:

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (98) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1048) Python Coding Challenge (456) Python Quiz (122) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)