0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can also use the ticker API matplotlib.org/api/ticker_api.html#tick-locating which lets you specify how the tick locations are decided in a more flexible way. ex ax.get_yaxis().set_major_locator(MultipleLocator(1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.