添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在写论文画图的时候,我们会对图片的标题,图例等文字部分设定规定好的字体和样式。

比如,我们给所有文字都设定好了Times New Roman字体,但是Times New Roman字体是一个比较特殊的存在,有两点

  1. 跟系统默认字体有冲突
  2. 字体粗细无法改

一个例子,我们使用plt画图,设置字体,应用字体

# 定义字体font1
font1 = {'family': 'Times New Roman',
'weight': 'normal',
'size': 10,
plt.figure(figsize=(8, 6))
epochs = range(0, len(tmodel_train))
A, = plt.plot(epochs, a, color="saddlebrown", linestyle="-", marker="o" , label='this is a ')
B, = plt.plot(epochs, b, color="mediumpurple", linestyle="-", marker="o" , label='this is b')
C, = plt.plot(epochs, c, color="dimgray", linestyle="-", marker="o" , label='this is c')
D, = plt.plot(epochs, d, color="y", linestyle="-", marker="o" , label='this is d')
# 将字体应用到图例上
legend = plt.legend(handles=[A,B,C,D], prop=font1)
plt.xlabel('Epochs', font1)
plt.gca().set_ylim(0.85,1.0053)
plt.ylabel('Accuracy', font1)
plt.legend()
plt.savefig('demo.pdf')

结果是,图例的字体并不是我们想要的Times New Roman。即图片中字体不统一。

一、解决字体不一致的方法

采用全局字体配置

将全局字体改为Times New Roman:

import matplotlib.pyplot as plt
plt.rc('font',family='Times New Roman')

二、解决Times New Roman加粗的问题

在字体设置中,weight负责字体的粗细。用默认的字体时,weight的变化是可以改变字体粗细的。

但是一旦把字体设定为"Times New Roman",weight就没办法调节说明框中字体的粗细。默认使用Times New Roman的时候,字体会是加粗状态。这就是问题的所在,也是比较麻烦的一个地方。

解决方案是,添加一行语句:

del matplotlib.font_manager.weight_dict['roman']
matplotlib.font_manager._rebuild()

即可将Times New Roman字体变细。

3.放置tff文件 找到Anaconda环境下的对应虚拟环境matplotlib目录,放置下载的simsun.tff文件。 D:\Program Files (x86)\Anaconda3\envs\jupyter\Lib\site-packages\matplotlib\mpl-data\fonts\ttf 4.修改matplo 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$)' from matplotlib import rcParams config = { # "font.family":'Times New Roman', # 设置字体类型 "font.size": 80, # "mathtext.font
Python画图设置宋体和新罗马Times New Roman 相信很多用Python进行画图的小伙伴会有困惑,每次画出来的图都是黑体,粘贴到Word里面,和其他的文字也很不搭,但是又懒得改,主要是我一直也没找到很有效的方法,但今天偶然学到一个方法,觉得很有用,就分享给大家 import matplotlib.pyplot as plt from matplotlib import rcParams config = { "font.family": 'serif',
python中用matplotlib库画图时,把中文设置为宋体,英文设置为Time New Roman,有时候还需要显示公式。设置方法如下: import matplotlib.pyplot as plt from matplotlib import rcParams import numpy as np config = { "font.family":'serif', "font.size": 18, "mathtext.fontset":'stix', "font.s
对于使用 Python 进行图形绘制,你可以使用 Matplotlib 库。你可以通过以下代码设置图中中文字体为仿宋_GB2312,英文字体Times New Roman: import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 设置仿宋_GB2312字体 zh_font = fm.FontProperties(fname='/path/to/simfang.ttf') # 设置Times New Roman字体 en_font = fm.FontProperties(fname='/path/to/times.ttf') # 绘制图形 plt.plot([0, 1, 2, 3], [1, 2, 3, 4]) plt.title("标题", fontproperties=zh_font, fontsize=14) plt.xlabel("横轴", fontproperties=zh_font, fontsize=12) plt.ylabel("纵轴", fontproperties=zh_font, fontsize=12) plt.show() 需要注意的是,请确保你已经安装了仿宋_GB2312和 Times New Roman 字体,并且路径设置正确。
语法错误 SyntaxError: from __future__ imports must occur at the beginning of the file 的分析和解决