Karnivaurus
Karnivaurus

Reputation: 24121

Set the aspect ratio of a plot in matplotlib

Using Matplotlib, I want to draw six plots side-by-side. However, I want each plot to have an aspect ratio of 1.

If I run the following:

import matplotlib.pyplot as plt
fig = plt.figure()

for n in range(1, 6):
    fig.add_subplot(1, 6, n)
    plt.axis([0, 4, 0, 4])

plt.show()

Then it shows the six plots "squashed" along the x-axis. This occurs even though I have set the x-axis and the y-axis to be the same length.

How can I make all the plots have an aspect ratio of 1?

Upvotes: 0

Views: 10329

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

With 5 plots side by side, you must set the figure size to allow for enough space for your plots, and add a bit of padding between plots so the text labels of the axis of one subplot do not overlap the next plot.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,2))

for n in range(1, 6):
    ax = fig.add_subplot(1, 5, n)
    ax.set_aspect(1)
    plt.axis([0, 4, 0, 4])

plt.tight_layout(pad=1)

plt.show()

enter image description here

Upvotes: 5

Related Questions