Pandas secondary_y: 如何将图例放置在情节之外?

Pandas secondary_y: How to place legends outside plot?

当我在 Pandas 中使用“secondary_y”时,我很难移动图例的位置。下面的代码给出了一个例子:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'C' : [4,5,6,7], 'S' : [10,20,30,40],'R' : [100,50,-30,-50]})
fig, ax = plt.subplots()
df[['C', 'S', 'R']].plot.bar(ax=ax, rot=90,  secondary_y= ['S', 'R'])

当不使用“secondary_y”时,以下工作

handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='lower left', ncol=3,
           bbox_to_anchor=(0.25, -.175))

但是当我使用“secondary_y”时上面的最后一个代码不起作用。

有人知道怎么得到图下面的图例吗?

我在 找到的以下内容有效

h1, l1 = ax.get_legend_handles_labels()
h2, l2 = ax.right_ax.get_legend_handles_labels()
handles = h1+h2
labels = l1+l2
ax.legend(handles, labels, loc='lower left', ncol=3,
       bbox_to_anchor=(0.25, -.575))