Python Matplotlib Y-Axis在Plot右侧打勾

时间:2022-10-19 07:35:07

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?

我有一个简单的线图,需要将y轴刻度从绘图的(默认)左侧移动到右侧。有关如何做到这一点的任何想法?

4 个解决方案

#1


147  

Use ax.yaxis.tick_right()

使用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()

Python Matplotlib Y-Axis在Plot右侧打勾

#2


80  

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

对于正确的标签,请使用ax.yaxis.set_label_position(“right”),即:

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()

#3


44  

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:

joaquin的回答是有效的,但是具有从轴的左侧去除刻度的副作用。要解决此问题,请通过调用set_ticks_position('both')来跟踪tick_right()。修改后的例子:

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.

结果是两侧都有刻度的图,但右侧是刻度标签。

Python Matplotlib Y-Axis在Plot右侧打勾

#4


10  

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

只是有人问(就像我做的那样),当使用subplot2grid时这也是可能的。例如:

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:

它会显示:

Python Matplotlib Y-Axis在Plot右侧打勾

#1


147  

Use ax.yaxis.tick_right()

使用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()

Python Matplotlib Y-Axis在Plot右侧打勾

#2


80  

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

对于正确的标签,请使用ax.yaxis.set_label_position(“right”),即:

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()

#3


44  

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:

joaquin的回答是有效的,但是具有从轴的左侧去除刻度的副作用。要解决此问题,请通过调用set_ticks_position('both')来跟踪tick_right()。修改后的例子:

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.

结果是两侧都有刻度的图,但右侧是刻度标签。

Python Matplotlib Y-Axis在Plot右侧打勾

#4


10  

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

只是有人问(就像我做的那样),当使用subplot2grid时这也是可能的。例如:

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:

它会显示:

Python Matplotlib Y-Axis在Plot右侧打勾