I can add a y label to the left y-axis using plt.ylabel
, but how can I add it to the secondary y-axis?
table = sql.read_frame(query,connection)
table[0].plot(color=colors[0],ylim=(0,100))
table[1].plot(secondary_y=True,color=colors[1])
plt.ylabel('$')
5 Answers 5
The best way is to interact with the axes
object directly
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 *y1
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')
plt.show()
example graph
-
1How to get the right y axis like the left one, I mean, from bottom to top, from 0 to 5, aligned.Sigur– Sigur2018年03月22日 01:43:05 +00:00Commented Mar 22, 2018 at 1:43
-
How to rotate the blue text without overlapping the ticks?Sigur– Sigur2018年03月22日 01:45:53 +00:00Commented Mar 22, 2018 at 1:45
-
@Sigur you have to mess with passing the horizontalalignment and/or verticalalignment parameter to ax2.set_ylabelPaul H– Paul H2018年03月22日 05:17:10 +00:00Commented Mar 22, 2018 at 5:17
-
@PaulH, I found that we can get the y limits from ax1 and set it up to ax2, so the position of labels will be aligned.Sigur– Sigur2018年03月22日 13:19:04 +00:00Commented Mar 22, 2018 at 13:19
-
5Sigur first question: ax2.set_ylim(ax.get_ylim()) Sigur second question: ax2.set_ylabel('Y2 data', rotation=0, labelpad=<int>) Hope this helps someone out.wellplayed– wellplayed2018年04月16日 17:21:09 +00:00Commented Apr 16, 2018 at 17:21
There is a straightforward solution without messing with matplotlib: just pandas.
Tweaking the original example:
table = sql.read_frame(query,connection)
ax = table[0].plot(color=colors[0],ylim=(0,100))
ax2 = table[1].plot(secondary_y=True,color=colors[1], ax=ax)
ax.set_ylabel('Left axes label')
ax2.set_ylabel('Right axes label')
Basically, when the secondary_y=True
option is given (eventhough ax=ax
is passed too) pandas.plot
returns a different axes which we use to set the labels.
I know this was answered long ago, but I think this approach worths it.
-
1Thanks - great approach! However, it is worth noting that this only works if you plot on the primary y-axis first, then the secondary y-axis, exactly as you have done. If you switch the order, it misbehaves.user667489– user6674892018年01月23日 16:45:55 +00:00Commented Jan 23, 2018 at 16:45
For everyone stumbling upon this post because pandas gets mentioned,
you now have the very elegant and straightforward option of directly accessing the
secondary_y axis in pandas with ax.right_ax
So paraphrasing the example initially posted, you would write:
table = sql.read_frame(query,connection)
ax = table[[0, 1]].plot(ylim=(0,100), secondary_y=table[1])
ax.set_ylabel('$')
ax.right_ax.set_ylabel('Your second Y-Axis Label goes here!')
-
8This is a matplotlib feature, not a pandas featurePaul H– Paul H2021年01月15日 17:09:17 +00:00Commented Jan 15, 2021 at 17:09
Simple example with few loc:
plot(y1)
plt.gca().twinx().plot(y2, color = 'r') # default color is same as first ax
Explanation:
ax = plt.gca() # Get current axis
ax2 = ax.twinx() # make twin axis based on x
ax2.plot(...) # ...
I don't have access to Python right now, but off the top of my head:
fig = plt.figure()
axes1 = fig.add_subplot(111)
# set props for left y-axis here
axes2 = axes1.twinx() # mirror them
axes2.set_ylabel(...)