当前位置:脚本大全 > > 正文

python读取mat文件(详解Python Matplot中文显示完美解决方案)

时间:2022-01-14 02:35:49类别:脚本大全

python读取mat文件

详解Python Matplot中文显示完美解决方案

原因与现象

matplot是一个功能强大的python图表绘制库,很遗憾目前版本自带的字体库中并不支持中文字体。所以如果在绘制内容中需要显示中文,那么就会显示为方格字符。

解决办法

有一个较为完美的解决方案,通过扫描matplot自带字体库以及系统字体库,寻找能够支持的中文字体,如果能够找到的话,就设置第一个为matplot的字体熟悉。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • import matplotlib.pyplot as plt
  • from matplotlib.font_manager import fontmanager
  • from pylab import mpl
  • import subprocess
  •  
  • def get_matplot_zh_font():
  •  fm = fontmanager()
  •  mat_fonts = set(f.name for f in fm.ttflist)
  •  
  •  output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=true)
  •  zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
  •  available = list(mat_fonts & zh_fonts)
  •  
  •  print '*' * 10, '可用的字体', '*' * 10
  •  for f in available:
  •  print f
  •  return available
  •  
  • def set_matplot_zh_font():
  •  available = get_matplot_zh_font()
  •  if len(available) > 0:
  •  mpl.rcparams['font.sans-serif'] = [available[0]] # 指定默认字体
  •  mpl.rcparams['axes.unicode_minus'] = false # 解决保存图像是负号'-'显示为方块的问题
  • 在绘图之前,调用set_matplot_zh_font()设置一下就可以了。

    效果

    如下图所示:

    python读取mat文件(详解Python Matplot中文显示完美解决方案)

    以上所述是小编给大家介绍的python matplot中文显示完美解决方案详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!

    原文链接:https://blog.csdn.net/kesalin/article/details/71214038

    上一篇下一篇

    猜您喜欢

    热门推荐