Math Pi irrational Number Graph Plotting Visualization in Python

Math Pi irrational Number Graph Plotting Visualization in Python

In the world of mathematics and physics, visualizations play a crucial role in understanding complex concepts and phenomena. In this blog post, we'll explore how to create stunning animated visualizations using Python's Matplotlib library.



Understanding the Code

1. Let's start by dissecting the code snippet provided above. This code creates an animated visualization of a mathematical function known as the Pitot tube. The Pitot tube is commonly used in fluid dynamics to measure fluid flow velocity.


2. Importing Libraries: The code begins by importing necessary libraries, including NumPy for numerical computations and Matplotlib for plotting.


3. Creating Theta Values: An array of theta values in degrees is created using NumPy's linspace function. These values represent the angle at which the function is evaluated.


4. Converting Degrees to Radians: The theta values are then converted from degrees to radians using NumPy's deg2rad function. This conversion is necessary because trigonometric functions in Python work with radians.


5. Initializing the Plot: The plot is initialized with a black background using Matplotlib's subplots function.


6. Defining the Update Function: The update function is defined to update the plot for each frame of the animation. It clears the axis, sets the limits and aspect ratio, and calculates the complex function z(theta) using the Pitot tube formula.


7. Creating the Animation: The FuncAnimation class is used to create the animation. It takes the figure, update function, number of frames, and interval as arguments.


8. Saving the Animation: Finally, the animation is saved as an MP4 video file named pitot_tube_animation2.mp4 with a frame rate of 30 frames per second.


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Create an array of theta values in degrees (e.g., from 0 to 113*360 degrees)
theta_degrees = np.linspace(0, 113*360, 1000000)

# Convert degrees to radians
theta_radians = np.deg2rad(theta_degrees)

# Initialize the plot
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_facecolor('black')

# Function to update the plot for each frame
def update(frame):
    ax.clear()
    ax.set_xlim(-2.5, 2.5)
    ax.set_ylim(-2.5, 2.5)
    ax.set_aspect('equal')
    ax.set_facecolor('black')
    ax.grid(False)

    # Calculate z(theta) using the formula
    z = np.exp(theta_radians[:frame] * 1j) + np.exp(np.pi * theta_radians[:frame] * 1j)

    # Separate the real and imaginary parts of z
    x = np.real(z)
    y = np.imag(z)

    # Plot the data
    ax.plot(x, y, color='white', linewidth=0.5)

# Create the animation
animation = FuncAnimation(fig, update, frames=len(theta_degrees), interval=20)

# Display the animation
plt.close()  # Prevents displaying the static plot before the animation
animation.save('pitot_tube_animation2.mp4', fps=30)

Post a Comment

0 Comments