python - matplotlib 或 seaborn 中的 utf-8 字符打印

标签 python matplotlib utf-8 seaborn

我有 utf-8(印地语)词向量并想要可视化。它使用 t-SNE,我使用 matplotlib 的注释方法来绘制单词。

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1,2]
y = [4,2]
hindi_word = [u'के', u'है']
for i, txt in enumerate(hindi_word):
    ax.annotate(txt, (x[i],y[i]))

ax.scatter(x,y)

它显示的是方框,而不是实际的单词“के”、“है”,如下图所示。如何打印实际的 utf-8 单词而不是方框。

output image

最佳答案

您需要更改排版的字体。 标准字体不支持您想要的字符。 我在 matplotlib 中发现了一个可以解决问题的方法:Lohit Devanagari。我不确定这是唯一的可能性,这是第一个有效的。

在注释之前,添加此行以更改字体:

plt.rc('font', family='Lohit Devanagari')

Matplotlib 现在呈现正确的字符:

output

关于python - matplotlib 或 seaborn 中的 utf-8 字符打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013971/

相关文章:

python - 错误 : not all arguments converted during bytes formatting

python - 使用 Sublime 清理 Python 代码

Python 间距和对齐字符串

python - 如何将 args 从 test 传递到 setUp() 方法以进行 Python 单元测试?

python - 在 Jupyter Notebook 中使用 matplotlib 绘制动态变化的图形

java - 在java中检测汉字

python - Networkx 图形绘制节点权重

python - 是否可以检测 matplotlib 艺术家是否适合与 colorbar 一起使用?

java - RandomAccessFile 读取西里尔文 UTF-8 java

php - TCPDF UTF-8 符号未显示