matplotlib科研绘图---Times New Roman字体设置

matplotlib科研绘图—Times New Roman字体设置

一、法一

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()

二、法二

解决字体不一致的方法
将全局字体改为Times New Roman

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

解决Times New Roman加粗的问题

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

三、一些参数

matplotlib说明框中字体粗细
用默认的字体时,'weight’的变化是可以改变字体粗细的,以下是代码

import matplotlib.pyplot as plt

styles=['normal','italic','oblique']
weights=['ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
plt.figure(figsize=(15,3)) 
for i in range(len(styles)):
    for j in range(len(weights)):
        font={'style':styles[i],'weight':weights[j]}
        plt.subplot(len(styles),len(weights),i*len(weights)+j+1) 
        plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'b', label='VGG-16')
        plt.legend(loc='upper right', prop=font)
plt.savefig('style.png')

一旦把字体设定为’Times New Roman’,'weight’就没办法调节说明框中字体的粗细

import matplotlib.pyplot as plt

styles=['normal','italic','oblique']
weights=['ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black']
plt.figure(figsize=(15,3)) 
for i in range(len(styles)):
    for j in range(len(weights)):
        font={'family' : 'Times New Roman','style':styles[i],'weight':weights[j]}
        plt.subplot(len(styles),len(weights),i*len(weights)+j+1) 
        plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'b', label='VGG-16')
        plt.legend(loc='upper right', prop=font)
plt.savefig('style.png')

四、Matplotlib画各种论文图

# coding=utf-8

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['Arial']  # 如果要显示中文字体,则在此处设为:SimHei
plt.rcParams['axes.unicode_minus'] = False  # 显示负号

x = np.array([1, 2, 3, 4, 5, 6])
VGG_supervised = np.array([2.9749694, 3.9357018, 4.7440844, 6.482254, 8.720203, 13.687582])
VGG_unsupervised = np.array([2.1044724, 2.9757383, 3.7754183, 5.686206, 8.367847, 14.144531])
ourNetwork = np.array([2.0205495, 2.6509762, 3.1876223, 4.380781, 6.004548, 9.9298])

# label在图示(legend)中显示。若为数学公式,则最好在字符串前后添加"$"符号
# color:b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、
# 线型:-  --   -.  :    ,
# marker:.  ,   o   v    <    *    +    1
plt.figure(figsize=(10, 5))
plt.grid(linestyle="--")  # 设置背景网格线为虚线
ax = plt.gca()
ax.spines['top'].set_visible(False)  # 去掉上边框
ax.spines['right'].set_visible(False)  # 去掉右边框


plt.plot(x, VGG_supervised, marker='o', color="blue", label="VGG-style Supervised Network", linewidth=1.5)
plt.plot(x, VGG_unsupervised, marker='o', color="green", label="VGG-style Unsupervised Network", linewidth=1.5)
plt.plot(x, ourNetwork, marker='o', color="red", label="ShuffleNet-style Network", linewidth=1.5)

group_labels = ['Top 0-5%', 'Top 5-10%', 'Top 10-20%', 'Top 20-50%', 'Top 50-70%', ' Top 70-100%']  # x轴刻度的标识
plt.xticks(x, group_labels, fontsize=12, fontweight='bold')  # 默认字体大小为10
plt.yticks(fontsize=12, fontweight='bold')
# plt.title("example", fontsize=12, fontweight='bold')  # 默认字体大小为12
plt.xlabel("Performance Percentile", fontsize=13, fontweight='bold')
plt.ylabel("4pt-Homography RMSE", fontsize=13, fontweight='bold')
plt.xlim(0.9, 6.1)  # 设置x轴的范围
plt.ylim(1.5, 16)

# plt.legend()          #显示各曲线的图例
plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize=12, fontweight='bold')  # 设置图例字体的大小和粗细

plt.savefig('./filename.svg', format='svg')  # 建议保存为svg格式,再用inkscape转为矢量图emf后插入word中
plt.show()

matplotlib科研绘图---Times New Roman字体设置_第1张图片

你可能感兴趣的:(科研绘图,python)