Matplotlib is a comprehensive 2D plotting library for Python. It enables the creation of a wide variety of static, animated, and interactive plots and charts for data visualization. Matplotlib is often used in conjunction with other libraries such as NumPy for numerical operations and Pandas for data manipulation.
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
x = np.linspace(0, 2 * np.pi, 100)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Create a figure and axis
fig, ax = plt.subplots()
# Plot the sine and cosine functions
ax.plot(x, y_sin, label='Sin(x)')
ax.plot(x, y_cos, label='Cos(x)')
# Customize the plot
ax.set_title('Sine and Cosine Functions')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
# Display the plot
plt.show()
This Python example demonstrates creating a simple plot with Matplotlib:
Feel free to run this script in your Python environment to visualize the sine and cosine functions. You can customize the code and explore other plot types and customization options provided by Matplotlib.
To run this script, you'll need to have Matplotlib and NumPy installed. You can install them using the following command:
pip install matplotlib numpy