Matplotlib and NumPy Basics Reference Guide for Skulpt
1. Introduction to Matplotlib and NumPy in Skulpt
Matplotlib is a plotting library for creating static, animated, and interactive visualisations in Python. NumPy is a library for numerical computations, providing support for arrays and mathematical functions. In this online implementation of python, a subset of Matplotlib and NumPy functionalities are available for creating simple plots directly in the browser.
2. Importing Matplotlib and NumPy
Before plotting, you need to import the required libraries:
import matplotlib.pyplot as plt import numpy as np
3. Creating Basic Plots
You can create basic line plots using Matplotlib.
Example: Plotting a Simple Line Graph
x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Line Graph') plt.show()
[Add image of the line graph here]
4. Using NumPy Arrays
NumPy arrays are used for efficient numerical computations.
Creating NumPy Arrays
import numpy as np a = np.array([1, 2, 3, 4, 5]) print(a)
Output:
[1 2 3 4 5]
5. Plotting Mathematical Functions
You can plot mathematical functions using NumPy and Matplotlib.
Example: Plotting a Sine Wave
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('sin(x)') plt.title('Sine Wave') plt.show()
[Add image of the sine wave plot here]