0

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?

5
  • Hmm, you could set the figaspect so as to have control over the figure aspect ratio to start with. Commented Oct 15, 2015 at 17:28
  • Try this: import matplotlib.pyplot as plt fig = plt.figure() for n in range(1, 6): a=fig.add_subplot(1, 6, n) a.set_aspect(1) plt.axis([0, 4, 0, 4]) Commented Oct 15, 2015 at 17:34
  • @AleksanderLidtke please don't put long pieces of code in comments. And for that matter, don't answer questions in comments. Make an answer. Commented Oct 15, 2015 at 17:40
  • Have you seen matplotlib.org/examples/pylab_examples/axis_equal_demo.html ? In particular, note the call to axis('equal'). Commented Oct 15, 2015 at 18:02
  • And matplotlib.org/examples/pylab_examples/equal_aspect_ratio.html Commented Oct 15, 2015 at 18:05

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.