shaifali Gupta
shaifali Gupta

Reputation: 388

Change ticks of y-axis in python

I want to change y axis ticks in python. I am using the code

import pylab as plt
y1 = [0,1,2,...10]
y2 = [90,40,65,12,....]
labels = [0.30,0.29,0.28,....]
plt.plot(y1)
plt.plot(y2,'r')
plt.yticks(y1, labels)
plt.yticks(y2, labels)
plt.show()

But all the y axis label appear at one place on top of one another

Upvotes: 0

Views: 871

Answers (1)

Will Elson
Will Elson

Reputation: 341

Borrowing heavily from this example, the code below demonstrates one possible way to have two plots on one figure.

import pylab as plt

fig, ax1 = plt.subplots()
y1 = [0,1,2,3,4,5,6,7,8,9,10]

labels = [0.30,0.29,0.28,0.27,0.26,0.25,0.24,0.23,0.22,0.21,0.20]
ax1.plot(labels, y1, 'b-')

ax1.set_xlabel('labels')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('y1', color='b', rotation="horizontal")
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
y2 = [90,40,65,12,23,43,54,13,42,53,63]
ax2.plot(labels, y2, 'r-')
ax2.set_ylabel('y2', color='r', rotation="horizontal")
ax2.tick_params('y', colors='r')

fig.tight_layout()
plt.show()

Upvotes: 1

Related Questions