Instagram
youtube
Facebook
Twitter

Use NumPy Arrays in Plotting

Description:

A Python program that uses NumPy arrays to generate and visualize mathematical data using Matplotlib.

Code Explanation:

np.linspace(0, 10, 100) creates an array of 100 evenly spaced numbers from 0 to 10.
np.sin(x) computes the sine of each value in array x.
plt.plot() plots the sine wave using NumPy-generated data.
color, linestyle, and linewidth are used to customize the line.
plt.title(), plt.xlabel(), and plt.ylabel() add context to the chart.
plt.grid(True) enables grid lines for better readability.

Program:

import numpy as np
import matplotlib.pyplot as plt

# Generate data using NumPy
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create the plot
plt.figure(figsize=(6, 4))
plt.plot(x, y, color='green', linestyle='-', linewidth=2)

# Add titles and labels
plt.title("Sine Wave Using NumPy Arrays")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)

# Show the plot
plt.tight_layout()
plt.show()

 

Output: