One article to get Python matplotlib to quickly draw a line chart (multiple subgraphs, coordinate axis scale range, font settings)

1. Python matplotlib draws a line chart (multiple subgraphs, axis scale range)

Tutorials without illustrations are hooligans

If you want to draw the following picture, please continue to read

sample graph

First look at the drawing diagram, this picture mainly completes the following points (see the comparison picture)

  1. Draw a line chart
  2. Draw a line chart with multiple subplots
  3. Control axis ticks, ranges, labels
  4. Control the font format of the whole image
  5. The coordinate axis is marked red, and the vertical line is drawn out

Look at the code directly, there are comments, select according to your needs

import matplotlib.pyplot as plt

# 设置字体格式
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 14,
}


# 选取画布,figsize控制画布大小
fig = plt.figure(figsize=(7,5))
# fig = plt.figure()

# 绘制子图 1,2,1 代表绘制 1x2 个子图,本图为第 1 个,即 121
# ax 为本子图
ax = fig.add_subplot(1, 2, 1)

# 横纵坐标
x1 = [8,16,24,32,48,64]
y1 = [72.59, 78.78, 82.83, 81.56, 76.09, 67.81]

# 绘图 # 具体线型、颜色、label可搜索该函数参数
ax.plot(x1, y1,'o-' ,color='g' ,label="MBVIL")  

# ax 子图的 x,y 坐标 label 设置
# 使用 r'...$\gamma$' 的形式可引入数学符号
# font 为label字体设置
ax.set_xlabel(r'(a) scale factor $\gamma$', font1)
ax.set_ylabel("mAP(%) on VeRi-776", font1)

# 最小值
minv = 64

# 在x1[2]处,从最小值到y1[2]引一条垂直虚线
# 线型控制参数具体搜索
ax.vlines(x1[2], minv, y1[2], linestyles='dashed', colors='lightcoral', linewidth=1)

# x 坐标的刻度和标签设置
# 以 16 为间隔绘制刻度
x_interval = [(16.* i) for i in range(0, 5)]

# 由于不包含24,所以将 24 放入刻度并排序
x_interval.append(24)
x_interval.sort()

# 设置刻度
ax.set_xticks(x_interval) # 设置x刻度

# 可以更改为 log 间距
# ax.set_xscale('log', base=2)

# 刻度替换: 用其他值代替刻度值
# ax.set_xticklabels(x1)
# ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'], rotation=30, fontsize=12)

# x轴范围限制
# plt.xlim()

# 同理 y 轴进行处理
y_interval = [(60. + i) for i in range(0, 28, 4)]
ax.set_yticks(y_interval)
# 设置 y 范围
ax.set_ylim((minv, 84))

# 线型的标签绘制, loc 控制位置, prop 控制字体
# ax.legend(loc = 'lower right', prop=font1)

# 坐标轴字体设置
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]

# 坐标轴字体颜色,将第2个设为红色
ax.get_xticklabels()[2].set_color("red")


# 添加子图 2,具体方法与图 1 差别不大
ax2 = fig.add_subplot(1, 2, 2)
x2 = [0.2, 0.3, 0.4, 0.45, 0.5, 0.55, 0.6]#点的横坐标
y2 = [75.63, 75.56, 78.66, 80.89, 82.67, 80.42, 73.16]#线2的纵坐标
# plt.plot(x,k1,'s-',color = 'r',label="红线的名字")#s-:方形
ax2.plot(x2, y2,'o-' ,color='green' ,label="MBVIL")  #o-:圆形
ax2.vlines(x2[-3], minv, y2[-3], linestyles='dashed', colors='lightcoral', linewidth=1)
ax2.set_xlabel(r'(b) relaxation factor $m$', font1)
ax2.set_yticks(y_interval)
ax2.set_ylim((minv, 84))
labels = ax2.get_xticklabels() + ax2.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
ax2.get_xticklabels()[-3].set_color("red")

# 保存及展示
#plt.savefig('hyperparams.eps')
plt.show()

Guess you like

Origin blog.csdn.net/qq_40491305/article/details/117381798