Ameya Kulkarni
Ameya Kulkarni

Reputation: 198

Set scale of axis in plot using matplotlib

I am unable to scale the y-axis. My code is as follows:

import matplotlib.pyplot as pt
import numpy as np
fig = pt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)
pt.plot(x,y)
pt.show()

The y axis has scale of 5. I'm trying to make it 1. The output snapshot

Upvotes: 0

Views: 8855

Answers (1)

Srivatsan
Srivatsan

Reputation: 9363

You can do so using set_yticks this way:

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)

ax.plot(x,y)
ax.set_yticks(np.arange(min(y), max(y)+1, 1.0)) # setting the ticks
ax.set_xlabel('x')
ax.set_ylabel('y')
fig.show()

Which produces this image wherein y-axis has a scale of 1.

enter image description here

Upvotes: 2

Related Questions