2

I'm trying to to draw a plot with Matplotlib, but I have a problem in adjusting the distance between two axes intervals simultaneously. I have written a very simple code like the one by ubuntu:

import numpy as np
import matplotlib.pyplot as plt
x = [0,5,9,10,15]
y = [0,1,2,3,4]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.yticks(np.arange(min(y), max(y)+1, 1.0))
plt.show()

I got this plot. I am wondering if there is a way to program the distance between intervals in y and x axes simultaneously. I need to adjust the distance between xticks and yticks simultaneously according their interval distance not the axis alone. Here I need to change the first plot to a new plot like this one. Although in the first plot the distance between xticks and yticks are both 1.0, but we can see that the distance is longer in y axis but shorter in x axis. I need y and x axis distances to be like each other. For example, if the intervals between xticks is 1.0 and yticks is 2.0, the interval between yticks gets twice distance comparing to xticks. Is there anyone having a solution for this problem?

sara
  • 23
  • 1
  • 5

1 Answers1

3

You can override the aspect ratio of the axis using the set_aspect method. In your case, you want the aspect ratio to be 1.

x = [0,5,9,10,15]
y = [0,1,2,3,4]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.yticks(np.arange(min(y), max(y)+1, 1.0))
ax = plt.gca() # gets the active axis
ax.set_aspect(1)
plt.show()

enter image description here

James
  • 32,991
  • 4
  • 47
  • 70