Created
November 14, 2022 03:31
-
-
Save davidliyutong/197ee2f5e1cb1103f6a614cfce9a3b51 to your computer and use it in GitHub Desktop.
Python code to plot dim-time relation of four sorting algorithms using matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
plt.rc('font',family='Times New Roman') # 使用Times New Roman | |
# 定义标题和坐标 | |
titles = ['Selection sort', 'Insertion sort', 'Bubble sort', 'Quick sort'] | |
x_ticks = [10, 100, 1000, 10000, 100000] | |
x_labels = ['10', '100', '1k', '10k', '100k'] | |
# 填充数据 | |
data_avg = { | |
"selection": [1,2,3,4,5], | |
"insertion": [1,2,3,4,5], | |
"bubble": [1,2,3,4,5], | |
"quick": [1,2,3,4,5], | |
} | |
data_min = { | |
"selection": [1,1,1,1,1], | |
"insertion": [1,1,1,1,1], | |
"bubble": [1,1,1,1,1], | |
"quick": [1,1,1,1,1], | |
} | |
# 创建图像,绘制子图 | |
fig = plt.figure(figsize=(10, 10), dpi=150) | |
ax = fig.add_subplot(221) | |
plt.plot(x_ticks, data_avg['selection'], marker='o', color="blue",label="average") | |
plt.plot(x_ticks, data_min['selection'], marker='o',linestyle='dashed', color="blue",label="min") | |
ax.set_xlabel('dim', fontsize=20) | |
ax.set_ylabel('$t_{avg}$', fontsize=12) | |
ax.set_xticks(x_ticks) | |
ax.set_xscale('log') | |
ax.set_title(titles[0], fontsize=20) | |
plt.legend() | |
plt.grid('on') | |
ax = fig.add_subplot(222) | |
plt.plot(x_ticks, data_avg['insertion'], marker='o', color="blue",label="average") | |
plt.plot(x_ticks, data_min['insertion'], marker='o',linestyle='dashed', color="blue",label="min") | |
ax.set_xlabel('dim', fontsize=20) | |
ax.set_ylabel('$t_{avg}$', fontsize=12) | |
ax.set_xticks(x_ticks) | |
ax.set_xscale('log') | |
ax.set_title(titles[1], fontsize=20) | |
plt.legend() | |
plt.grid('on') | |
ax = fig.add_subplot(223) | |
plt.plot(x_ticks, data_avg['bubble'], marker='o', color="blue",label="average") | |
plt.plot(x_ticks, data_min['bubble'], marker='o',linestyle='dashed', color="blue",label="min") | |
ax.set_xlabel('dim', fontsize=20) | |
ax.set_ylabel('$t_{avg}$', fontsize=12) | |
ax.set_xticks(x_ticks) | |
ax.set_xscale('log') | |
ax.set_title(titles[2], fontsize=20) | |
plt.legend() | |
plt.grid('on') | |
ax = fig.add_subplot(224) | |
plt.plot(x_ticks, data_avg['quick'], marker='o', color="blue",label="average") | |
plt.plot(x_ticks, data_min['quick'], marker='o',linestyle='dashed', color="blue",label="min") | |
ax.set_xlabel('dim', fontsize=20) | |
ax.set_ylabel('$t_{avg}$', fontsize=12) | |
ax.set_xticks(x_ticks) | |
ax.set_xscale('log') | |
ax.set_title(titles[3], fontsize=20) | |
plt.legend() | |
plt.grid('on') | |
plt.tight_layout(pad=0.4, w_pad=1, h_pad=1.0) | |
plt.savefig("compare.png") # 位图格式 | |
plt.savefig("compare.svg") # 矢量格式 | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment