Diego Sacconi
Diego Sacconi

Reputation: 120

Subplots with same axis size having set aspect='equal'

Probably this has already been asked somewhere else but I couldn't find the answer. I'm sorry if this is the case.

How can I get subplots with the same axis sizes? I need aspect='equal', so that the relative distances are correctly scaled.

This is the code I'm using

X_A = [0, 0.4, 0.8, 1, 0.2, 0.5, 0.3]
Y_A = [0.3, 0.8, 0, 1, 0.8, 0.2, 0.9]

X_B = [0, 0.4, 0.8, 1, 0.2, 0.5, 0.3]
Y_B = [2, 0, 0.8, 3, 3, 2.1, 0.2]
fig, axes = plt.subplots(1, 2, figsize=(10,10))

ax1 = plt.subplot(121)
ax1.set_aspect('equal')
ax1.set_title('Dataset A', fontsize=20)

ax1.scatter(X_A, Y_A)

ax2 = plt.subplot(122)
ax2.set_aspect('equal')
ax2.set_title('Dataset B', fontsize=20)

ax2.scatter(X_B, Y_B)

I'm getting this: enter image description here

There is some simple setting to tune or some math is required?

Upvotes: 4

Views: 6127

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

You would want to use the adjustable argument to set_aspect,

ax.set_aspect('equal', adjustable="datalim")

This would not let the axes box be adjusted, but rather the limits of the axes.

Complete example,

import matplotlib.pyplot as plt

X_A = [0, 0.4, 0.8, 1, 0.2, 0.5, 0.3]
Y_A = [0.3, 0.8, 0, 1, 0.8, 0.2, 0.9]

X_B = [0, 0.4, 0.8, 1, 0.2, 0.5, 0.3]
Y_B = [2, 0, 0.8, 3, 3, 2.1, 0.2]

fig, (ax, ax2) = plt.subplots(1, 2, figsize=(6,6))

ax.set_aspect('equal', adjustable="datalim")
ax.set_title('Dataset A', fontsize=20)

ax.scatter(X_A, Y_A)

ax2.set_aspect('equal', adjustable="datalim")
ax2.set_title('Dataset B', fontsize=20)

ax2.scatter(X_B, Y_B)

ax2.autoscale()

plt.show()

producing

enter image description here

Upvotes: 5

Related Questions