2
Lesson 2
Basic structure of a plot
Matplotlib's pyplot module is a collection of functions that makes it easy to create and manage plots. It provides a MATLAB-like interface, which simplifies creating and controlling figures, axes, and plots.
Key Functions in pyplot:
Key Functions in pyplot:
- plot(): For drawing basic line plots.
- show(): For displaying the figure in a window or notebook.
- figure(): For creating a new figure.
- subplot(): For creating multiple plots within a single figure.
- title(), xlabel(), ylabel(): For adding labels and titles to your plots.
Example of Creating a basic plot:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a line plot plt.plot(x, y) # Display the plot plt.show()
In this simple example, plt.plot() is used to create the line plot, and plt.show() is used to display it.
Understanding figures and axes
In Matplotlib, everything that is drawn goes onto a figure. The figure serves as the container for everything that will appear on the plot. A figure can contain multiple axes (plural of axis). Each axis corresponds to a specific plot (or chart) within the figure.
Key components:
- Figure: The overall window or page that everything is drawn on. A figure can contain one or more axes.
- Axes: These are the individual plots that appear within a figure. Each plot has its own set of x and y axes.
Think of a figure as the canvas, and the axes as the individual plots on that canvas.
a. Creating a basic figure and axes:
Matplotlib automatically creates a figure and a set of axes when you call a plot function like plot(). However, you can also manually create them if you want more control over how the plots are displayed.
import matplotlib.pyplot as plt # Create a figure and a set of axes fig, ax = plt.subplots() # Sample data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Create a plot on the axes ax.plot(x, y) # Add labels and title ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_title('Basic Plot') # Display the plot plt.show()
Explanation of code:
- fig, ax = plt.subplots() creates a figure (fig) and a set of axes (ax).
- ax.plot() creates the plot on the axes.
- ax.set_xlabel(), ax.set_ylabel(), and ax.set_title() are used to add labels and a title to the axes.
b. Multiple axes in a figure:
You can also create multiple plots (axes) within a single figure using plt.subplots() and specifying the number of rows and columns.
import matplotlib.pyplot as plt # Create a figure with 1 row and 2 columns of axes fig, (ax1, ax2) = plt.subplots(1, 2) # Data for the first plot x1 = [1, 2, 3, 4] y1 = [1, 4, 9, 16] # Data for the second plot x2 = [1, 2, 3, 4] y2 = [2, 3, 5, 7] # Create the first plot ax1.plot(x1, y1) ax1.set_title('Plot 1') # Create the second plot ax2.plot(x2, y2) ax2.set_title('Plot 2') # Display both plots plt.show()
In this example:
- We create a figure with two plots (axes) side by side using plt.subplots(1, 2).
- ax1 and ax2 correspond to the two separate axes, allowing us to display two different plots within the same figure.
The Anatomy of a plot
Each plot (or axes) in Matplotlib has several key components that you can customize:
- Axes: The area where the data is plotted (also known as the subplot).
- X-axis and Y-axis: The horizontal and vertical axes, which can be customized (e.g., limits, labels, ticks).
- Title: A descriptive label for the plot.
- Labels: Descriptions for the x-axis and y-axis.
Here's a visual breakdown:
- Figure: The outer container that holds everything.
- Axes: The area inside the figure where the actual plot appears.
- Legend: A box that describes the plotted data, usually helpful when multiple lines are plotted.
Conclusion
In this lesson, we explored the basic structure of a plot in Matplotlib. We covered the pyplot module, the concept of figures and axes, and how to create single and multiple plots. In the next lesson, we will dive deeper into customizing plots with titles, labels, colors, and more advanced features.