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.
0 Comments:
Post a Comment