Setting the aspect ratio
When preparing figures for a journal publication or a website, one might need a figure that has one specific aspect ratio. In this recipe, we are going to see how to control the aspect ratio of a figure.
How to do it...
The pyplot
API provides a simple way to set up a custom aspect ratio, as follows:
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-6, 6, 1024) Y1, Y2 = np.sinc(X), np.cos(X) plt.figure(figsize=(10.24, 2.56)) plt.plot(X, Y1, c='k', lw = 3.) plt.plot(X, Y2, c='.75', lw = 3.) plt.show()
The aspect ratio of the following figure is much different from what we would get by default:
How it works...
We use the pyplot.figure()
function, which creates a new Figure
instance. A Figure
object represents a figure as a whole. Usually, this object is created implicitly, behind the scenes. However, by creating the object explicitly, we can control various aspects of a figure, including its aspect ratio. The figsize
parameter allows us to specify its...