I have an exotic task for Matplpoltlib for which I need to display emoji inside labels. I am using OS X, so there is a good system-wide support of emoji. However, things do not work out with Matplotlib: currently, emoji get displayed as empty boxes. I am using Python 3, so Unicode should not be a problem. Thank you 😀
1 Answer
The idea behind it is not very difficult but in order to get it to be displayed (at least on ubunto) it gave me some hard time as not all fonts support emoji. I shall display the straight forward way but for some of the steps I had to make are detailed here in case you encounter any difficulties.
From emoji cheat sheet from the emoji python package I picked up three to be shown as an example and here is the code.
G = nx.Graph()
G.add_nodes_from([0,1,2])
n0 = emoji.emojize(':thumbsup:',use_aliases=True)
n1 = emoji.emojize(':sob:',use_aliases=True)
n2 = emoji.emojize(':joy:',use_aliases=True)
labels ={0:n0,1:n1,2:n2}
nx.draw_networkx(G,labels=labels, node_color = 'w', linewidths=0, with_labels=True, font_family = 'Symbola' ,font_size = 35)
plt.show()
-
If this question is a duplicate of the linked question (aka if the same answer can apply to both questions), then please flag this question as a duplicate instead of leaving an answer to that effect. If it is not a duplicate, please leave a complete answer instead of a link-only answer.– josliber ♦Oct 19, 2015 at 0:50
-
1That was my first thought (I am not still an expert with how to deal with situations on SOF). But the thing is this question was asked before the question I answered, so at that time there was no answer. So, my hunch was it would mean that the asked did not make enough effort to find an existing solution which was not there. Oct 19, 2015 at 0:56
-
But I guess copying the complete answer here is not the best reaction also. This means I should flag it as a duplicate right ? Oct 19, 2015 at 0:57
-
It is OK to flag an older question as a duplicate of a newer one if the newer one has better answers. Since that seems to be the case here, that sounds like the best course of action.– josliber ♦Oct 19, 2015 at 0:57
-
1Michael links a few genuine [Answers] of his choice, that are pretty delightfull on their own. Take a joy on 'em. There was no intention to interpret his observations, but to present an adult experience and a solid view about what you have just experienced for the first time on your own. The value is in the insights you, Abdallah, definitely have, the rest is a disturbing noise. Unpleasant, but just a noise. Oct 20, 2015 at 14:54
prop = fm.FontProperties(fname='/System/Library/Fonts/Apple\ Color\ Emoji.ttf')
matplotlib.rcParams['font.family'] = prop.get_name()
Results in "Could not set the fontsize for facefile"