Setting Up the Axes
In this lesson, we will learn to modify the axes to fit our needs.
We'll cover the following...
Python provides the functionality to modify the axes to provide high-quality graphs.
Axes range #
One of the most important things we need to configure is the axes of a graph. There are 3 possible ways:
-
Set range by default.
-
For tight-fitting, use
axis('tight')
on the axis object. -
Use
set_ylim([ymin, ymax])
andset_xlim([xmin, xmax])
methods on the axis object to set a custom range.ymin
andymax
are the lower and upper bounds for the y-axis.xmin
andxmax
are the lower and upper bounds for the x-axis.
The following code implements the above-mentioned ways:
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-5, 5, 100)fig, axes = plt.subplots(3, 1, figsize=(12, 8))axes[0].plot(x, 2 * x + 2, x, 2 * x + 4)axes[0].set_title("default axes")axes[1].plot(x, 2 * x + 2, x, 2 * x + 4)axes[1].axis('tight')axes[1].set_title("tight axes")axes[2].plot(x, 2 * x + 2, x, 2 * x + 4)axes[2].set_ylim([-10, 10]) # setting xaxes[2].set_xlim([-2, 2])axes[2].set_title("custom axes");fig.tight_layout()
Logarithmic scale
Scientists and engineers like to work with linear relationships as they are easier to work with. When there are non-linear relationships with large values, it becomes imperative to use the logarithmic scale as it makes the data look cleaner and easy to work with.
Using matplotlib
, it is possible to set one or both axes to a logarithmic scale using the following methods:
set_yscale('log')
set_xscale('log')
Let’s look at the implementation of the following equation:
...