Python 绘图
常用的 Python 绘图工具是 Matplotlib
折线图 plot
import matplotlib.pyplot as plt
# 设置字体族。英文使用 Times New Roman,中文使用 SimSun
plt.rcParams['font.family'] = ['Times New Roman', 'SimSun']
# 数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# 创建绘图
plt.plot(x, y, marker='o', linestyle='-', color='b', label='Line 1')
# 添加标题和标签
plt.title('简单线图 Simple Line Plot')
plt.xlabel('X 轴 X Axis')
plt.ylabel('Y 轴 Y Axis')
# 显示图例
plt.legend()
# 显示图形
plt.show()
散点图 scatter
import matplotlib.pyplot as plt
x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])
# 绘制数据点
plt.scatter(x_train, y_train, marker='x', c='r')
# 设置标题
plt.title("Housing Prices")
# 设置 Y 轴标签
plt.ylabel('Price (in 1000s of dollars)')
# 设置 X 轴标签
plt.xlabel('Size (1000 sqft)')
plt.show()
设置字体
为每个元素单独设置字体:
import matplotlib.pyplot as plt
times = {'fontname':'Times New Roman'}
cmmi10 = {'fontname':'cmmi10'} # Computer Modern Math Italic
plt.title('title',**times)
plt.xlabel('xlabel', **cmmi10)
plt.show()
设置全局字体:
import matplotlib.pyplot as plt
# 正文字体
plt.rcParams['font.family'] = ['Times New Roman', 'SimSun']
# 数学字体
plt.rcParams['mathtext.fontset'] = 'stix'
数学公式
plot(x, y, label=f'$Z_n=\\frac{{S_n − n\\mathbf{{E}}X}}{{\\sqrt{{n\\mathbf{{D}}X}}}}$')
参考:How to change fonts in matplotlib (python)? | Stack Overflow
更多示例
折线图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Times New Roman', 'SimSun'] # 设置字体族
plt.rcParams['axes.unicode_minus'] = False # 显示负号
x = np.array([3,5,7,9,11,13,15,17,19,21])
A = np.array([0.9708, 0.6429, 1, 0.8333, 0.8841, 0.5867, 0.9352, 0.8000, 0.9359, 0.9405])
B = np.array([0.9708, 0.6558, 1, 0.8095, 0.8913, 0.5950, 0.9352, 0.8000, 0.9359, 0.9419])
C = np.array([0.9657, 0.6688, 0.9855, 0.7881, 0.8667, 0.5952, 0.9361, 0.7848, 0.9244, 0.9221])
D = np.array([0.9664, 0.6701, 0.9884, 0.7929, 0.8790, 0.6072, 0.9352, 0.7920, 0.9170, 0.9254])
# 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) # 去掉右边框
# A 图
plt.plot(x, A, color="black", label="A algorithm", linewidth=1.5)
# B 图
plt.plot(x, B, "k--", label="B algorithm", linewidth=1.5)
# C 图
plt.plot(x, C, color="red", label="C algorithm", linewidth=1.5)
# D 图
plt.plot(x, D, "r--", label="D algorithm", linewidth=1.5)
# 设置标题
plt.title("example", fontsize=12, fontweight='bold')
# 设置坐标轴刻度标识
x_ticks=['dataset1', 'dataset2', 'dataset3', 'dataset4', 'dataset5', ' dataset6', 'dataset7', 'dataset8', 'dataset9', 'dataset10'] # X 轴刻度的标识
plt.xticks(x, x_ticks, fontsize=12, fontweight='bold')
plt.yticks(fontsize=12, fontweight='bold')
# 设置坐标轴标签
plt.xlabel("Data sets", fontsize=13, fontweight='bold')
plt.ylabel("Accuracy", fontsize=13, fontweight='bold')
# 设置坐标轴范围
plt.xlim(3,21)
# plt.ylim(0.5,1)
# 显示图例
# 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') # 建议保存为 svg 格式,再用 inkscape 转为矢量图 emf 后插入 word 中
plt.show()