Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib y-axis label on right side

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.

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.

like image 433
apdnu Avatar asked Nov 13 '12 22:11

apdnu


People also ask

How do I put the Y-axis on the right side in Python?

To shift the Y-axis ticks from left to right, use ax. yaxis. tick_right() where ax is axis created using add_subplot(xyz) method.

Which Python code places a label along the y-axis in Matplotlib?

pyplot. ylabel. Set the label for the y-axis.

How do I change the location of a legend in Python?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.


2 Answers

It looks like you can do it with:

ax.yaxis.set_label_position("right") ax.yaxis.tick_right() 

See here for an example.

like image 99
Gerrat Avatar answered Sep 21 '22 03:09

Gerrat


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 :

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

like image 39
Gourav Mahapatra Avatar answered Sep 17 '22 03:09

Gourav Mahapatra