a11
a11

Reputation: 3396

Matplotlib simple way to set axes tick intervals equal?

Is there a one-liner way, that works in subplots, to set my x-axis tick interval equal to my y-axis tick interval?

There are a bunch of questions out there on setting ticks in matplotlib, but they all seem to involve rebuilding the tick arrays.

import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
x = [-1,5,8,2,1,1,4]
y = [0,5,-1,7,-3,3,2]
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(311)
ax1.scatter(x,y);
ax1.set_aspect('equal')

enter image description here

Upvotes: 2

Views: 294

Answers (1)

Amit Amola
Amit Amola

Reputation: 2510

On top of my head the only solution I can think of is this(it's 3 line or rather 2 line though):

>>> import matplotlib.pyplot as plt
>>> import matplotlib.ticker as ticker

>>> plt.style.use('seaborn-whitegrid')
>>> x = [-1,5,8,2,1,1,4]
>>> y = [0,5,-1,7,-3,3,2]
>>> fig = plt.figure(figsize=(10,10))
>>> ax1 = plt.subplot(311)
>>> ax1.scatter(x,y)
>>> ax1.set_aspect('equal')

>>> tick_spacing = 2  #You can skip adding this and put 2 in place in below lines
>>> ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
>>> ax1.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

enter image description here

Please do share if you find another way of doing this in the comments to this answer.

Upvotes: 1

Related Questions