...

/

Setting Up the Axes

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:

  1. Set range by default.

  2. For tight-fitting, use axis('tight') on the axis object.

  3. Use set_ylim([ymin, ymax]) and set_xlim([xmin, xmax]) methods on the axis object to set a custom range.

    • ymin and ymax are the lower and upper bounds for the y-axis.
    • xmin and xmax are the lower and upper bounds for the x-axis.

The following code implements the above-mentioned ways:

Press + to interact
import numpy as np
import matplotlib.pyplot as plt
x = 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 x
axes[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:

  1. set_yscale('log')
  2. set_xscale('log')

Let’s look at the implementation of the following equation:

y=e2xy=e^{2x} ...