Doubt in plotting legend

Here in code below we have filtered our data for some states as visible in legend. But if i use command plt.legend( ) Its showing all states in the legend however we have already modified our dataframe. Why is it happening so?

fig = plt.gcf();
fig.set_size_inches(15, 6);
sns.lineplot('date', 'confirmed', hue="state", data=df_,
             palette='Reds', hue_order = ['wb', 'gj', 'ka', 'dl', 'tn', 'mh']);

1 Like

Hi @SahilBansal,
By default, plt.legend() takes in all the labels by its own. If you’re looking to filter out some of them, and just show a few of them, you can try out the following:

plt.legend((line1, line2, line3), (label1, label2, label3))

Doing so, we’re specifically defining only the elements which should be included in our legend.
For reference, please take a look at matplotlib.pyplot.legend

1 Like