I'm ploting a categorical data and value count by sns.countplot()
I'm trying to add legend for x-values to the figure as following: handles is set of x-value, labels is the descriptions of x-values.
ax = sns.countplot(x = df.GARAGE_DOM)
handles, labels = ax.get_legend_handles_labels()
handles = ["VP", "BC", "GC", "GP", "JC", "PO"]
labels = ["Voie Publique", "box", "Garage couvert", "garage particulier clos", "Jardin clos", "parking ouvert"]
by_label = OrderedDict(zip(handles,labels))
ax.legend(by_label.keys(), by_label.values())
However, I got warning that
UserWarning:
Legend does not support 'VP' instances. A proxy artist may be used instead. See: http://matplotlib.org/users/legend_guide.html#using-proxy-artist
I've read the doc of proxy artist but I didn't find examples in my case.
Thanks for your help.
Here is a possible solution, creating a text field as a legend handler. The following would create a
TextHandler
to be used to create the legend artist, which is a simplematplotlib.text.Text
instance. The handles for the legend are given as tuples of (text, color) from which theTextHandler
creates the desiredText
.The above solution is an updated version of the original version below, which seems more complicated. The following is the original solution, which uses a
TextArea
and anAnchoredOffsetbox
to place the text inside the legend.