Jason Strimpel
Jason Strimpel

Reputation: 15506

Y-Axis ticks on Right Side of Plot

I have a simple line plot and need to move the y-axis ticks from the (default) left side of the plot to the right side. Any thoughts on how to do this?

Upvotes: 153

Views: 208121

Answers (5)

Tony Power
Tony Power

Reputation: 1188

Using subplots and if you are sharing the y-axis (i.e., sharey=True), before creating the plot, try:

plt.rcParams['ytick.right'] = plt.rcParams['ytick.labelright'] = True plt.rcParams['ytick.left'] = plt.rcParams['ytick.labelleft'] = False

From: Matplotlib gallery

Upvotes: 2

Titianne
Titianne

Reputation: 1463

Just is case somebody asks (like I did), this is also possible when one uses subplot2grid. For example:

import matplotlib.pyplot as plt
plt.subplot2grid((3,2), (0,1), rowspan=3)
plt.plot([2,3,4,5])
plt.tick_params(axis='y', which='both', labelleft='off', labelright='on')
plt.show()

It will show this:

enter image description here

Upvotes: 28

Tom Baldwin
Tom Baldwin

Reputation: 1008

joaquin's answer works, but has the side effect of removing ticks from the left side of the axes. To fix this, follow up tick_right() with a call to set_ticks_position('both'). A revised example:

from matplotlib import pyplot as plt

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_ticks_position('both')
plt.plot([2,3,4,5])
plt.show()

The result is a plot with ticks on both sides, but tick labels on the right.

enter image description here

Upvotes: 70

Dietrich
Dietrich

Reputation: 5551

For right labels use ax.yaxis.set_label_position("right"), i.e.:

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
plt.plot([2,3,4,5])
ax.set_xlabel("$x$ /mm")
ax.set_ylabel("$y$ /mm")
plt.show()

Upvotes: 137

joaquin
joaquin

Reputation: 85683

Use ax.yaxis.tick_right()

for example:

from matplotlib import pyplot as plt

f = plt.figure()
ax = f.add_subplot(111)
ax.yaxis.tick_right()
plt.plot([2,3,4,5])
plt.show()

enter image description here

Upvotes: 263

Related Questions