简体   繁体   English

matplotlib:用表情符号标签注释情节

[英]matplotlib: annotate plot with Emoji labels

I'm using Python 3.4 in macOS. 我在macOS中使用Python 3.4。 Matplotlib is supposed to support Unicode in labels, but I'm not seeing Emojis rendered properly. Matplotlib应该在标签中支持Unicode,但是我看不到Emojis的正确渲染。

import matplotlib.pyplot as plt
# some code to generate `data` and `labels`...
plt.clf()
plt.scatter(data[:, 0], data[:, 1], c=col)
# disclaimer: labeling taken from example http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot
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'))
plt.show(False)

结果

A few of the old pre-Unicode Emojis show up in their old style, but the rest (in this example, "fire," "music," and others) don't. 少数旧的Unicode之前的Emoji表情以旧样式显示,但其余的(在此示例中为“ fire”,“ music”等)则没有。 Is there a trick to make these appear properly? 有没有技巧可以使它们正确显示?

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. plt.annotate函数中,可以添加参数fontname来指定对表情符号有良好支持的字体。

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. 以下代码是我在Windows机器上通过对代码进行一些编辑后得到的代码,似乎“ Segoe UI Emoji”已安装在我的计算机上。

# this line is for jupyter notebook
%matplotlib inline

import 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'] = 300
data = 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', # this is the param added
        fontsize=20)
plt.show()

Here is what I got, the emojis may not show clearly, it depends on your typeface: 这是我得到的,表情符号可能无法清楚显示,这取决于您的字体: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM