Matplotlib: combine legend with same color and name
<p>If I repeat the plot with same color and label name, the label would appear multiple times:</p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')

x_labels = [10,20,30]
x = [1,2,3,4]
y = [3,1,5,1]

for label in x_labels:
    x_3d = label*np.ones_like(x)
    ax.plot(x_3d, x, y, color='black', label='GMM')

ax.legend()
</code></pre>

<p><a href="https://i.stack.imgur.com/J7eAB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/J7eAB.png" alt="enter image description here"></a></p>

<p>Is it possible to make them into one, combing the same lables legends into one? Something like</p>

<p><a href="https://i.stack.imgur.com/RoexB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RoexB.png" alt="enter image description here"></a></p>

<p>I can produce the above pic by </p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')

x_labels = [10,20,30]
x = [1,2,3,4]
y = [3,1,5,1]
legend = False

for label in x_labels:
    x_3d = label*np.ones_like(x)
    ax.plot(x_3d, x, y, color='black', label='GMM')
    if legend == False:
        ax.legend()
        legend = True
</code></pre>

<p>But this feels very ugly, is there any good sotion? Or do I simply make the plot in a wrong way?</p>