matplotlib字体设置

整理自以下两篇文章:1. https://blog.csdn.net/Vinsuan1993/article/details/78511870

                                    2.https://blog.csdn.net/love__live1/article/details/83143195

 

步骤:

1、由于matplotlib默认不支持ttc,所以可以将ttc转换ttf先。将Windows字体 simsun.ttc上传到 https://transfonter.org/ttc-unpack 在线转换成TTF,
2、得到simsun.ttf和nsimsun.ttf,将两个ttf文件放到PYTHON安装目录的 Lib\site-packages\matplotlib\mpl-data\fonts\ttf 子目录下。

例如:我的电脑上,如下:C:\Users\vinsuan\AppData\Local\Programs\Python\Python35\Lib\site-packages\matplotlib\mpl-data\fonts\ttf
3、删除字体缓存以便重新生成字体缓存:清空$HOME/.matplotlib/文件夹下的所有文件及文件夹,如果不放心,请先备份!!!

例如:我的电脑上,如下:C:\Users\vinsuan\.matplotlib

全局字体设置代码示例:

#coding:utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun'
 
x, y = np.loadtxt('data.txt', delimiter=',', unpack=True)
plt.plot(x,y, '+',label='PoW算法',color='black')
 
plt.xlabel('难度值')
plt.ylabel('时间')
plt.title('PoW共识的hash难题解决时间表')
plt.legend()
plt.show()

部分参数需要设置其他字体,如新罗马示例:

plt.figure(figsize=[15,8])
plt.scatter(X, Y, label = 'RealValue')
plt.plot(X, func(X, a, b), 'red', label = 'CurveLine')
plt.title(station, fontdict={'family' : 'Times New Roman', 'size'   : 16})
plt.ylabel('Clocks($\mu S$)', fontdict={'family' : 'Times New Roman', 'size'   : 16})
plt.xlabel('Time', fontdict={'family' : 'Times New Roman', 'size'   : 16})
plt.yticks(fontproperties = 'Times New Roman', size = 14)
plt.xticks(fontproperties = 'Times New Roman', size = 14)
plt.legend(prop={'family' : 'Times New Roman', 'size'   : 16})
plt.savefig('./stationClocks/' + station + '.ps', dpi = 200)
plt.show()

或者 plt.text(4, 4, t, family='serif', style='italic', ha='right', wrap=True) 在某些函数里面添加参数family='serif',设置字体

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值