右侧为matplotlib y轴标签。

时间:2022-10-19 00:16:21

Is there a simple way to put the y-axis label on the right-hand side of the plot? I know that this can be done for the tick labels using ax.yaxis.tick_right(), but I would like to know if it can be done for the axis label as well.

有没有一种简单的方法把y轴标签放在图的右边?我知道这可以用ax.yaxis.tick_right()来做标记标签,但我想知道它是否也可以用于axis标签。

One idea which came to mind was to use

我想到的一个主意是使用。

ax.yaxis.tick_right()
ax2 = ax.twinx()
ax2.set_ylabel('foo')

However, this doesn't have the desired effect of placing all labels (tick and axis labels) on the right-hand side, while preserving the extent of the y-axis. In short, I would like a way to move all the y-axis labels from the left to the right.

然而,这并没有在右侧放置所有标签(标记和轴标签)的效果,同时保留y轴的范围。简而言之,我想要一种将所有y轴标签从左移到右的方法。

2 个解决方案

#1


57  

It looks like you can do it with:

看起来你可以这样做:

ax.yaxis.set_label_position("right")

See here for an example.

请看这里的一个例子。

#2


6  

If you would like to follow the example given in matplotlib and create a figure with labels on both sides of the axes but without having to use the subplots() function, here is my solution :

如果您想要遵循matplotlib中的示例,并创建一个带有两个坐标轴的标签的图形,但不需要使用submapping()函数,下面是我的解决方案:

from matplotlib import pyplot as plt
import numpy as np

ax1 = plt.plot()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
plt.plot(t,s1,'b-')
plt.xlabel('t (s)')
plt.ylabel('exp',color='b')

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
plt.ylabel('sin', color='r')
plt.show()

右侧为matplotlib y轴标签。

#1


57  

It looks like you can do it with:

看起来你可以这样做:

ax.yaxis.set_label_position("right")

See here for an example.

请看这里的一个例子。

#2


6  

If you would like to follow the example given in matplotlib and create a figure with labels on both sides of the axes but without having to use the subplots() function, here is my solution :

如果您想要遵循matplotlib中的示例,并创建一个带有两个坐标轴的标签的图形,但不需要使用submapping()函数,下面是我的解决方案:

from matplotlib import pyplot as plt
import numpy as np

ax1 = plt.plot()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
plt.plot(t,s1,'b-')
plt.xlabel('t (s)')
plt.ylabel('exp',color='b')

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
plt.ylabel('sin', color='r')
plt.show()

右侧为matplotlib y轴标签。