Given the following code:
import numpy as np
import matplotlib.pyplot as plt
import os, sys
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.legend() # oringinal legend
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.savefig('bar.png')
which returns this barplot:
I'd like to replace the legend ['Men', 'Women']
with emojis of and
which are saved in my directory from emojipedia.
To do so, I have the following class ImageHandler
added to my script, taken from here:
from matplotlib.transforms import Bbox, TransformedBbox
from matplotlib.legend_handler import HandlerBase
from matplotlib.image import BboxImage
class ImageHandler(HandlerBase):
def create_artists(self, legend, orig_handle, Xd_, Yd_, W_, H_, fontsize, trans):
# enlarge the image by these margins
sx, sy = self.image_stretch
# create a bounding box to house the image
bb = Bbox.from_bounds(Xd_ - sx, Yd_ - sy, W_ + sx, H_ + sy )
tbb = TransformedBbox(bb, trans)
image = BboxImage(tbb)
image.set_data(self.image_data)
self.update_prop(image, orig_handle, legend)
return [image]
def set_image(self, image_path, image_stretch=(0, 0)):
self.image_data = plt.imread(image_path)
self.image_stretch = image_stretch
Therefore, I replace ax.legend()
with:
emoji_dataset = os.path.join( os.environ['HOME'], 'Datasets', 'Emojis')
h1 = ImageHandler()
h2 = ImageHandler()
h1.set_image(os.path.join(emoji_dataset, 'man.png'), image_stretch=(0, 20))
h2.set_image(os.path.join(emoji_dataset, 'woman.png'), image_stretch=(0, 20))
ax.legend( handler_map={rects1: h1, rects2: h2},
handlelength=2, labelspacing=0.0,
fontsize=36, borderpad=0.15, loc='best',
handletextpad=0.2, borderaxespad=0.15)
However, I get the following error:
Traceback (most recent call last):
File "img_in_legend.py", line 117, in <module>
handletextpad=0.2, borderaxespad=0.15)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2886, in legend
return gca().legend(*args, **kwargs)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 290, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend.py", line 503, in __init__
self._init_legend_box(handles, labels, markerfirst)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend.py", line 767, in _init_legend_box
fontsize, handlebox))
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend_handler.py", line 117, in legend_artist
fontsize, handlebox.get_transform())
File "img_in_legend.py", line 48, in create_artists
self.update_prop(image, orig_handle, legend)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend_handler.py", line 74, in update_prop
self._update_prop(legend_handle, orig_handle)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend_handler.py", line 65, in _update_prop
self._default_update_prop(legend_handle, orig_handle)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/legend_handler.py", line 70, in _default_update_prop
legend_handle.update_from(orig_handle)
File "/home/xenial/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/artist.py", line 1133, in update_from
self._transform = other._transform
AttributeError: 'BarContainer' object has no attribute '_transform'
It seems that class ImageHandler
does not work with ax.bar()
in my sample example.
How could I add images to legend of barplot
in matplotlib
?
Cheers,