Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # matplotlib無法顯示中文字的問題
- import matplotlib
- import matplotlib.pyplot as plt
- from matplotlib import font_manager
- microhei = matplotlib.font_manager.FontProperties(fname='wqy-microhei.ttc')
- def get_all_text(obj):
- '''
- Get all text objects in a matplotlib Figure
- Helper for plt_show_ch
- '''
- queue = [obj]
- all_text = []
- while queue:
- currobj = queue.pop(0)
- if isinstance(currobj, matplotlib.text.Text):
- all_text.append(currobj)
- else:
- queue = queue + currobj.get_children()
- return all_text
- def plt_show_ch():
- '''代替 plt.show()
- 在 show 之前先把所有 Text 物件設定好用中文字體
- '''
- fig = plt.gcf()
- for textobj in get_all_text(fig):
- fontsize = textobj.get_fontsize()
- textobj.set_fontproperties(microhei)
- textobj.set_fontsize(fontsize)
- return plt.show()
Add Comment
Please, Sign In to add comment