如果我修改绘图的标签plt.legend([some labels]),然后我打电话,ax.get_legend_handles_labels()我会取回旧标签。
这是一个简单的例子:
In [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3], label='A')
Out[2]: [<matplotlib.lines.Line2D at 0x7f60749be310>]
In [3]: plt.plot([2,3,4], label='B')
Out[3]: [<matplotlib.lines.Line2D at 0x7f60749f1850>]
In [4]: ax = plt.gca()
In [5]: l = ax.get_legend_handles_labels()
In [6]: l
Out[6]:
([<matplotlib.lines.Line2D at 0x7f60749be310>,
<matplotlib.lines.Line2D at 0x7f60749f1850>],
[u'A', u'B'])
In [7]: plt.legend(['C', 'D']) ### This correctly modifies the legend to show 'C' and 'D', but then ...
Out[7]: <matplotlib.legend.Legend at 0x7f6081dce190>
In [10]: l = ax.get_legend_handles_labels()
In [11]: …Run Code Online (Sandbox Code Playgroud)