1. 引言
你是否也想问,CVPR论文中第一页的图是怎么画的,这里为你提供了代码,只需稍微的改动即可实现。
2. 效果图
3. 代码实现
- 没有注释,如图1所示
- 带有注释,如图2所示
代码如下:
3.1 不带注释 —— 如图1
import os.path
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Times New Roman']
"""
x: x轴的数据 —— 网络的速度
* x_A
* x_B
y: y轴的数据 —— 网络的top-1准确率
* y_A
* y_B
不同的形状代表的含义:网络的参数量
circle(圆形):
* circle_size
star(三角形):
* star_size
"""
"""====== [x -> 速度] ======"""
x_A = [100, 105, 111, 132, 108, 145]
x_B = [200, 222, 241, 210, 240, 207]
"""====== [y -> top-1准确率] ======"""
y_A = [96, 92, 92, 91, 95, 93]
y_B = [93, 90, 88, 89, 92, 87]
"""====== [circle -> 参数量] ======"""
# 如果大小不合适,调整factor
factor_A = 5
circle_size = [i * factor_A for i in [110, 70, 65, 90, 140, 65]]
"""====== [star -> 参数量] ======"""
# 如果大小不合适,调整factor
factor_B = 5
star_size = [i * factor_B for i in [55, 34, 30, 40, 60, 30]]
# 形状的颜色变化
color_size = [0, 1, 2, 3, 4, 5]
# 定义保存图片的路径
fig_path = "./res"
file_name = "examples_without_annotation.svg"
save_path = os.path.join(fig_path, file_name)
print(save_path)
if not os.path.exists(fig_path):
os.mkdir(fig_path)
"""====== [开始绘制] ======
https://matplotlib.org/stable/api/markers_api.html
"""
fig, ax = plt.subplots()
scatter_A = plt.scatter(x_A, y_A, c=color_size, s=circle_size, marker='o') # x, y, c多种网络圆形性能图——帮你冲击CVPR、ECCV、ICCVolor, shape
scatter_B = plt.scatter(x_B, y_B, c=color_size, s=star_size, marker="*") # x, y, color, shape
# 隐藏rt和r的框线
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# x和y轴的名称
plt.xlabel("x-axis/unit")
plt.ylabel("y-axis/unit")
plt.tight_layout()
"""
保存图片
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html?highlight=savefig#matplotlib.pyplot.savefig
"""
plt.savefig(save_path) # 保存图片
print(f"Figure has been saved in [{str(save_path)}]")
plt.show()
3.2 带有注释 —— 如图2
import os.path
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Times New Roman']
"""
x: x轴的数据 —— 网络的速度
* x_A
* x_B
y: y轴的数据 —— 网络的top-1准确率
* y_A
* y_B
不同的形状代表的含义:网络的参数量
circle(圆形):
* circle_size
star(三角形):
* star_size
"""
"""====== [x -> 速度] ======"""
x_A = [100, 105, 111, 132, 108, 145]
x_B = [200, 222, 241, 210, 240, 207]
"""====== [y -> top-1准确率] ======"""
y_A = [96, 92, 92, 91, 95, 93]
y_B = [93, 90, 88, 89, 92, 87]
"""====== [circle -> 参数量] ======"""
# 如果大小不合适,调整factor
factor_A = 5
circle_size = [i * factor_A for i in [110, 70, 65, 90, 140, 65]]
"""====== [star -> 参数量] ======"""
# 如果大小不合适,调整factor
factor_B = 5
star_size = [i * factor_B for i in [55, 34, 30, 40, 60, 30]]
"""====== [annotation的信息] ======"""
txt_v1 = ['A-1', 'A-2', 'A-3', 'A-4', 'A-5', 'A-6']
txt_v2 = ['B-1', 'B-2', 'B-3', 'B-4', 'B-5', 'B-6']
# 形状的颜色变化
color_size = [0, 1, 2, 3, 4, 5]
# 定义保存图片的路径
fig_path = "./res"
file_name = "examples_with_annotation.svg"
save_path = os.path.join(fig_path, file_name)
print(save_path)
if not os.path.exists(fig_path):
os.mkdir(fig_path)
"""====== [开始绘制] ======
https://matplotlib.org/stable/api/markers_api.html
"""
fig, ax = plt.subplots()
scatter_A = plt.scatter(x_A, y_A, c=color_size, s=circle_size, marker='o') # x, y, color, shape
scatter_B = plt.scatter(x_B, y_B, c=color_size, s=star_size, marker="*") # x, y, color, shape
# 隐藏rt和r的框线
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# x和y轴的名称
plt.xlabel("x-axis/unit")
plt.ylabel("y-axis/unit")
"""========================================= [net_1] ========================================="""
offset_A = 0.7
for i in range(len(circle_size)):
if i == 0: # 第一个圆
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] + 10, y_A[i] - 0.1),
xycoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
elif i == 1: # 第二个圆
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] - 10, y_A[i] - 1),
xycoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
elif i == 3: # 第4个圆
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] + 10, y_A[i] - 0.1),
xycoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
elif i == 4: # 第5个圆
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] + 4, y_A[i] - 1),
xycoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
elif i == 5: # 第6个圆
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] - 0.1, y_A[i] + 1.0),
xycoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
else:
plt.annotate(txt_v1[i],
xy=(x_A[i], y_A[i]),
xytext=(x_A[i] + offset_A, y_A[i] + offset_A),
xycoords='data',
# textcoords='offset pixels',
# textcoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
# fontsize=16,
# arrowprops=dict(arrowstyle='->, head_length=0.4, head_width=0.2', connectionstyle='arc3,rad=0')
# arrowprops=dict(arrowstyle='wedge, tail_width=0.3, shrink_factor=0.5', connectionstyle='arc3,rad=0')
# arrowprops=dict(arrowstyle='fancy, head_length=0.4, head_width=0.4, tail_width=0.4', connectionstyle='arc3,rad=0')
# arrowprops=dict(arrowstyle='simple, head_length=0.5, head_width=0.5, tail_width=0.2', connectionstyle='arc3,rad=0')
)
"""========================================= [net_2] ========================================="""
offset_B = 1
for i in range(len(star_size)):
plt.annotate(txt_v2[i],
xy=(x_B[i], y_B[i]),
xytext=(x_B[i] + offset_B, y_B[i] + offset_B),
xycoords='data',
# textcoords='offset pixels',
textcoords='data',
arrowprops=dict(arrowstyle='|-|, widthA=0,widthB=0', connectionstyle='arc3,rad=0', color="0.5"),
)
plt.tight_layout()
"""
保存图片
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html?highlight=savefig#matplotlib.pyplot.savefig
"""
plt.savefig(save_path) # 保存图片
print(f"Figure has been saved in [{str(save_path)}]")
plt.show()
4. 补充
- 代码很简单,无需讲解😂
- 输出图片格式可以调节,详情见Matplotlib文档