使用matplotlib和networkx在图形中显示unicode文本

2024-04-29 03:04:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图显示一个包含3个节点的图。节点的标签在天成文书中。我尝试在matplotlib中为一个天成文书单词使用字体属性,效果很好。当节点标签用于绘制图形时,它不显示这些标签。有人能找到在图表中显示天成文书文本的方法吗?我使用的是python2.7

#!/usr/bin/env python 
# -- coding: utf-8 -- 
%matplotlib notebook
import matplotlib.font_manager as fm 
import matplotlib.pyplot as plt 
import networkx as nx
prop = fm.FontProperties(fname='NotoSerifDevanagari-Regular.ttf') 
x=0.2
y=0.2
labels={}
graph = nx.Graph()

words= ["सनरायझर्स"," जैत ","जोडलें"]


for word in words:
   w = word.decode('utf-8')
   graph.add_node(w)
   labels[w]=w
graph.add_edge(words[0].decode('utf-8'),words[1].decode('utf-8'))
graph.add_edge(words[1].decode('utf-8'),words[2].decode('utf-8'))
pos=nx.spring_layout(graph)

nx.draw(graph,labels=words, with_labels = True)
plt.text(x, y, word, fontproperties=prop) 
plt.show()enter code here

Tags: importaddlabels节点matplotlibasplt标签