1

How do I change the limits on the axes in matplotlib subplots?

For example if I have

x = np.linspace(0,4*np.pi,100)

plt.figure(1)
plt.subplot(121)
plt.plot(x,np.sin(x))
plt.xlim([0,4*np.pi])
plt.ylim([-1,1])
plt.axis('equal')

plt.subplot(122)
plt.plot(x,np.cos(x))
plt.xlim([0,4*np.pi])
plt.ylim([-1,1])
plt.axis('equal')

I tried using plt.xlim and plt.ylim but these didn't work.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1123581321
  • 209
  • 2
  • 4
  • 9

1 Answers1

1

What you have works if you just remove the plt.axis('equal') lines.

EDIT: To answer your comment considering scales, this is the code that should work:

import numpy as np
import matplotlib.pyplot as plt   

x = np.linspace(0,4*np.pi,100)

plt.figure(1)
plt.subplot(121)
plt.plot(x,np.sin(x))
plt.xlim([0,4*np.pi])
plt.ylim([-1,1])
plt.gca().set_aspect('equal', adjustable='box')


plt.subplot(122)
plt.plot(x,np.cos(x))
plt.xlim([0,4*np.pi])
plt.ylim([-2*np.pi,2*np.pi])
plt.gca().set_aspect('equal', adjustable='box')

Output image showing proper scales

Solution courtesy of How to equalize the scales of x-axis and y-axis in Python matplotlib?

Hami
  • 704
  • 2
  • 9
  • 27