Python画图设置宋体和新罗马Times New Roman

Python画图设置宋体和新罗马Times New Roman

相信很多用Python进行画图的小伙伴会有困惑,每次画出来的图都是黑体,粘贴到Word里面,和其他的文字也很不搭,但是又懒得改,主要是我一直也没找到很有效的方法,但今天偶然学到一个方法,觉得很有用,就分享给大家

import matplotlib.pyplot as plt
from matplotlib import rcParams

config = {
    
    
            "font.family": 'serif',
            "font.size": 12,# 相当于小四大小
            "mathtext.fontset": 'stix',#matplotlib渲染数学字体时使用的字体,和Times New Roman差别不大
            "font.serif": ['SimSun'],#宋体
            'axes.unicode_minus': False # 处理负号,即-号
         }
rcParams.update(config)

在画图之前加上以上这段代码就OK了

x_data = ['2013','2014','2015','2016','2017','2018','2019','2020']  
y_data = [63000,71000,84000,90500,107000,120000,134000,145000] 
plt.figure(figsize=(9,8))
plt.plot(x_data, y_data, "r", marker='*', ms=10, label="a")
plt.xlabel("我是x轴", fontsize = 20)
plt.ylabel("我是y轴", fontsize = 20)
plt.title('我是标题', fontsize = 20)
plt.tick_params(labelsize=13)
plt.savefig('折线图.png', dpi=500, bbox_inches='tight') # 解决图片不清晰,不完整的问题
plt.show()

下面就是效果啦,很开心,希望能帮助正在困惑的你。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43697614/article/details/124219278