擅长:python、mysql、java
<p>正如<a href="http://matplotlib.org/users/legend_guide.html">docs</a>所说,虽然很容易错过:</p>
<blockquote>
<p>If label attribute is empty string or starts with “_”, those artists
will be ignored.</p>
</blockquote>
<p>所以如果我在一个循环中绘制相似的线,并且我只想要图例中的一条示例线,我通常会做如下操作</p>
<pre><code>ax.plot(x, y, label="Representatives" if i == 0 else "")
</code></pre>
<p>其中<code>i</code>是我的循环索引。</p>
<p>看起来并不像单独构建它们那么好,但是我经常希望标签逻辑尽可能接近于线图。</p>
<p>(请注意,<code>matplotlib</code>开发人员自己倾向于使用<code>"_nolegend_"</code>来显式地表示。)</p>