Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep x/y axes the same lengths in seaborn/matplotlib

I am generating some plots using matplotlib and seaborn, and the x-axis and y-axis are different lengths as shown in an example below.

enter image description here

Is there a way to ensure that each axis is of the same scale?

My example code is nothing special, but here is an example:

# plot data
plt.scatter(df['x'], df['y'], color = 'gray', s=5) # s controls point size
plt.xlim(-0.0002,0.002)
plt.ylim(-0.0002,0.002)

# add y=x line
plt.plot([0,1],[0,1], lw=2, color='#414242', linestyle='dashed')

sns.set_context("paper", font_scale=1.5)
plt.xlabel(xlabel, {'size':'20'})
plt.ylabel(ylabel, {'size':'20'})
plt.title(title)

sns.set_style('ticks')
sns.despine(offset=10, trim=True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Using plt.axis('equal'):

enter image description here

like image 976
The Nightman Avatar asked Aug 07 '18 19:08

The Nightman


1 Answers

I think you want plt.axis('equal'), see here.

like image 193
Bitwise Avatar answered Oct 23 '22 11:10

Bitwise