
Components of a Graph
Each graph has a set of common components that can be adjusted. The names that Matplotlib uses for these components are demonstrated in the following graph:

Figure 2.3: Components of a graph
The components of a graph are as follows:
- Figure: The base of the graph, where all the other components are drawn.
- Axis: Contains the figure elements and sets the coordinate system.
- Title: The title gives the graph its name.
- X-axis label: The name of the x-axis, usually named with the units.
- Y-axis label: The name of the y-axis, usually named with the units.
- Legend: A description of the data plotted in the graph, allowing you to identify the curves and points in the graph.
- Ticks and tick labels: They indicate the points of reference on a scale for the graph, where the values of the data are. The labels indicate the values themselves.
- Line plots: These are the lines that are plotted with the data.
- Markers: Markers are the pictograms that mark the point data.
- Spines: The lines that delimit the area of the graph where data is plotted.
Each of these components can be configured to adapt to the needs of the visualization task at hand. We will go through each type of graph and how to adapt the components as previously described.
Exercise 9: Creating a Graph
There are several ways to create a graph with Matplotlib. The first one is closely related to the MATLAB way of doing it, called Pyplot. Pyplot is an API that is a state-based interface for Matplotlib, meaning that it keeps the configurations and other parameters in the object itself. Pyplot is intended as a simple case.
Perform the following steps to create the graph of a sine function using the Matplotlib library:
- Import all the required libraries, as we did in the previous exercise:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
A second API, called object-oriented API, is intended for more complex plots, allowing for more flexibility and configuration. The usual way to access this API is to create a figure and axes using the plt.subplots module.
- Now, to get a figure and an axis, use the following command:
fig, ax = plt.subplots()
Note
The figure is the top container for all other graph components. The axes set things such as the axis, the coordinate system, and contain the plot elements, such as lines, text, and many more.
- To add a plot to a graph created using the object-oriented API, use the following command:
x = np.linspace(0,100,500)
y = np.sin(2*np.pi*x/100)
ax.plot(x, y)
The output is as follows:

Figure 2.4: Plot output using the object-oriented API
We are adding a line plot to the ax axis that belongs to the fig figure. Changes to the graph, such as label names, title, and so on, will be demonstrated later in this chapter. For now, let's look at how to create each kind of graph that we may use in an analysis.
Exercise 10: Creating a Graph for a Mathematical Function
In Exercise 1: Plotting an Analytical Function, we created a graph for a mathematical function using the MATLAB-like interface, Pyplot. Now that we know how to use the Matplotlib object-oriented API, let's create a new graph using it. Analysts have a lot of flexibility in creating graphs, whatever the data source, when using the object-oriented API.
Let's create a plot using the object-oriented API with the NumPy sine function on the interval [0,100]:
- Create the data points for the x-axis:
import numpy as np
x = np.linspace(0,100,200)
y = np.sin(x)
- Create the API interface for Matplotlib:
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
- Add the graph using the axis object, ax:
ax.plot(x, y)
The output is as follows:
Figure 2.5: Graph for a mathematical function
Notice that we again created a linear interval of values between [0, 100] using the linspace function, with 200 points. We then applied the sine function over these values to create the y axis. This is a common approach when creating data intervals.