Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to plot emojis in matplotlib?

Does anybody know how to plot emojis in matplotlib while using windows? I've been struggling to find a solution as most out there seem to be specific for macOS.

Below is my current graph showing emojis plotted in a vector space, but as usual most do not show up.

o

Is there perhaps any fonts already installed with matplotlib that provide emoji support or will I need to install some backend solutions?

Code:

def display_pca_scatterplot(model, words=None, sample=0):
    if words == None:
        if sample > 0:
            words = np.random.choice(list(model.vocab.keys()), sample)
        else:
            words = [ word for word in model.vocab ]

    prop = FontProperties(fname='/usr/share/fonts/truetype/noto/Apple Color Emoji.ttc')

    word_vectors = np.array([model[w] for w in words])

    twodim = PCA().fit_transform(word_vectors)[:,:2]

    sb.set_style("darkgrid")
    plt.figure(figsize=(10,10))
    plt.scatter(twodim[:,0], twodim[:,1]) #, edgecolors='w', color='w')
    for word, (x,y) in zip(words, twodim):
        plt.text(x+0.0, y+0.0, word, fontsize=20) #fontproperties=prop)
like image 385
Conor McNally Avatar asked Oct 27 '25 14:10

Conor McNally


1 Answers

I have created a small package imojify to deal with this issue

from imojify import imojify
from matplotlib import pyplot as plt 
from matplotlib.offsetbox import OffsetImage,AnnotationBbox

def offset_image(cords, emoji, ax):
    img = plt.imread(imojify.get_img_path(emoji))
    im = OffsetImage(img, zoom=0.08)
    im.image.axes = ax
    ab = AnnotationBbox(im, (cords[0], cords[1]),  frameon=False, pad=0)
    ax.add_artist(ab)


emjis = ['😂', '🤣', '😔', '😏','😍', '🥰', '😘']
values =[30, 50, 15, 29, 15, 50, 12]

fig, ax = plt.subplots(figsize=(12,8))
ax.bar(range(len(emjis)), values, width=0.5,align="center")
ax.set_xticks(range(len(emjis)))
ax.set_xticklabels([])
ax.tick_params(axis='x', which='major', pad=26)
ax.set_ylim((0, ax.get_ylim()[1]+10))

for i, e in enumerate(emjis):
    offset_image([i,values[i]+5], e, ax)

the package contains images for all emojis, imojify.get_img_path(emoji) simply returns the path of the emoji image then you can use OffsetImage to add these images as labels

like image 58
Abdul-Rahman Khayyal Avatar answered Oct 30 '25 07:10

Abdul-Rahman Khayyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!