Skip to content

Matplotlib - Simple Plot

This chapter demonstrates how to create a basic line plot using Matplotlib’s pyplot interface, which is convenient for quick visualizations.

  1. Import Libraries: We need matplotlib.pyplot for plotting functions and numpy for numerical operations and creating data arrays.
import matplotlib.pyplot as plt
import numpy as np
  1. Prepare Data: We need data for the x and y axes. Let’s create an array of angles and calculate their sine values.
# Generate x values (angles from 0 to 2*pi)
# np.linspace is preferred over np.arange for float steps
x = np.linspace(0, 2 * np.pi, 100) # 100 points between 0 and 2*pi
# Calculate y values (sine of x)
y = np.sin(x)
  1. Create the Plot: Use the plt.plot() function to plot y versus x.
plt.plot(x, y)
  1. Customize the Plot (Optional but Recommended): Add labels and a title to make the plot informative.
plt.xlabel("Angle [radians]")
plt.ylabel("Sine Value")
plt.title("Simple Sine Wave Plot")
plt.grid(True) # Add a grid for easier reading
  1. Display the Plot: Use plt.show() to open the Matplotlib viewer window and display the generated plot. This is necessary when running a script outside of environments like Jupyter Notebooks that handle display automatically.
plt.show()
import matplotlib.pyplot as plt
import numpy as np
# 1. Prepare Data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# 2. Create Figure and Plot
# Optional: Create figure explicitly for potentially more control later
plt.figure(figsize=(8, 5))
plt.plot(x, y, label='sin(x)', color='blue', linestyle='-')
# 3. Customize
plt.xlabel("Angle [radians]")
plt.ylabel("Sine Value")
plt.title("Simple Sine Wave Plot")
plt.grid(True)
plt.legend() # Display the label defined in plot()
# 4. Display
plt.show()

Executing this script will open a window displaying a standard sine wave plot with labeled axes, a title, a grid, and a legend.

When using Jupyter Notebooks or similar environments (like JupyterLab, Google Colab, VS Code Notebooks), plots can be displayed directly within the notebook output cells.

  1. Import Libraries: Same as above.
import matplotlib.pyplot as plt
import numpy as np
  1. Magic Command (Optional but common): The %matplotlib inline magic command tells the Jupyter kernel to render Matplotlib plots as static images embedded directly below the code cell that generated them. This is often the default behavior in modern Jupyter setups, but explicitly including it ensures consistency.
%matplotlib inline
  1. Prepare Data: Same as above.
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
  1. Plot and Customize: Use plt functions as before. With %matplotlib inline, you typically do not need to call plt.show(). The plot will automatically appear when the cell finishes execution.
plt.figure(figsize=(8, 5))
plt.plot(x, y, label='sin(x)', color='red', linestyle='--')
plt.xlabel("Angle [radians]")
plt.ylabel("Sine Value")
plt.title("Sine Wave in Jupyter")
plt.grid(True)
plt.legend()
# No plt.show() needed here with %matplotlib inline

Running this cell in a Jupyter Notebook will display the sine wave plot directly below the cell.