Book Image

matplotlib Plotting Cookbook

By : Alexandre Devert
Book Image

matplotlib Plotting Cookbook

By: Alexandre Devert

Overview of this book

Table of Contents (15 chapters)
matplotlib Plotting Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Scaling both the axes equally


By default, matplotlib will use a different scale for both the axes of a figure. In this recipe, we are going to see how to use the same scale for the two axes of a figure.

How to do it...

To accomplish this, we will need to play with the pyplot API and the Axes object, as shown in the following code:

import numpy as np
import matplotlib.pyplot as plt
T = np.linspace(0, 2 * np.pi, 1024)

plt.plot(2. * np.cos(T), np.sin(T), c = 'k', lw = 3.)
plt.axes().set_aspect('equal')

plt.show()

The preceding script draws an ellipse with its real aspect ratio, as follows:

How it works...

In this example, we display an ellipse where the major axis is twice the length of the minor axis. Indeed, the rendered ellipse follows those proportions.

The pyplot.axes() function returns an instance of the Axes object, the object in charge of the axes. The Axes instance have a set_aspect method, which we set to 'equal'. Now, both axes use the same scale. If we did not set the same aspect, the...