Oliver Moore
Oliver Moore

Reputation: 327

Putting two different entries under same label in a legend

I have plotted a graph with different data sets and trend lines. I was wondering if it was possible to have two different entries under the same label without the label appearing twice in the legend:

ax.plot(JD_inc, mag_inc, 'x', label = "Increasing Magnitude")
ax.plot(JD_dec, mag_dec, 'x', color = "forestgreen", label = "Decreasing Magnitude")

ax.plot(new_x_inc, new_y_inc, color = "orange", label = "Increasing Magnitude")
ax.plot(new_x_dec, new_y_dec, color = "crimson", label = "Decreasing Magnitude")


ax.errorbar(our_jul_day, our_mag, yerr = our_err, fmt = "x", color = "black", zorder = 10, label = "Our Data")

plt.legend()

is it also possible to remove the error bar from the "our data" label?

Here is an image of my current graph:

So, for example, is it possible to get the orange line and the blue point to essentially over lap in the legend?

Upvotes: 1

Views: 119

Answers (1)

Sameeresque
Sameeresque

Reputation: 2602

Based on the HandlerTuple example given in this link, the following should work in your case:

jd_inc_data, = ax.plot(JD_inc, mag_inc, 'x', label = "Increasing Magnitude")
new_x_inc_data, = ax.plot(new_x_inc, new_y_inc, color = "orange", label = "Increasing Magnitude")

plt.legend([(jd_inc_data, new_x_inc_data)], ["Increasing Magnitude"])

Upvotes: 2

Related Questions