Skip to content Skip to sidebar Skip to footer

Matplotlib: Annotate Plot With Emoji Labels

I'm using Python 3.4 in macOS. Matplotlib is supposed to support Unicode in labels, but I'm not seeing Emojis rendered properly. import matplotlib.pyplot as plt # some code to gene

Solution 1:

You problem here is that the default font have no good support for emojis.

In plt.annotate function, you can add a parameter fontname to specify the typeface that has a good support for emojis.

Following code are what I got on my Windows machine with some edits to your code, it seems that "Segoe UI Emoji" has been installed on my computer already.

# this line isfor jupyter notebook
%matplotlib inlineimport matplotlib.pyplot as plt
import numpy as np
# config the figure for bigger and higher resolution
plt.rcParams["figure.figsize"] = [12.0, 8.0]
plt.rcParams['figure.dpi'] = 300data = np.random.randn(7, 2)
plt.scatter(data[:, 0], data[:, 1])
labels = '😀 😃 😄 😁 😆 😅 😂 🤣 ☺️ 😊 😇'.split()
print(labels)
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
    plt.annotate(
        label, # some of these contain Emojis
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'),
        fontname='Segoe UI Emoji', # thisis the param added
        fontsize=20)
plt.show()

Here is what I got, the emojis may not show clearly, it depends on your typeface: enter image description here

Post a Comment for "Matplotlib: Annotate Plot With Emoji Labels"