繁体   English   中英

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

[英]matplotlib: annotate plot with Emoji labels

我在macOS中使用Python 3.4。 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)

结果

少数旧的Unicode之前的Emoji表情以旧样式显示,但其余的(在此示例中为“ fire”,“ music”等)则没有。 有没有技巧可以使它们正确显示?

您的问题是默认字体对表情符号没有很好的支持。

plt.annotate函数中,可以添加参数fontname来指定对表情符号有良好支持的字体。

以下代码是我在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()

这是我得到的,表情符号可能无法清楚显示,这取决于您的字体: 在此处输入图片说明

暂无
暂无

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

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