python - 使用折线图而不是条形图显示排序性能
问题描述
我正在尝试分析折线图中的合并排序排序性能,但仍然显示条形图,我该如何实现折线图中的图形?我正在使用for循环增加100,所以合并排序可以对数据进行排序,增加100。我已经在L中随机制作了10000大小的列表。
Tx = [0] * len(L) # time list
for i in range(0,len(L), 100):
start_time = time()
merge_sort(L[:i])
end_time = time()
elapsed_time = end_time - start_time
Tx[i] = elapsed_time * 1000
plt.plot(Tx, label='merge_sort')
plt.xlim(100, 10000)
plt.ylim(1, 10000)
plt.xlabel("n")
plt.ylabel('ms')
plt.yscale('log')
plt.legend(loc = "upper left")
plt.show()
解决方案
问题如下所示:
- 您的
Tx
. 其余所有值均为零,未定义日志。所以事情在线性尺度上看起来不错,但是当你在对数 y 尺度上绘制事物时,唯一的峰值出现在非零值上,因为它与-inf
log(0) 的值相连。因此,它对您来说就像一个酒吧。
为了说服自己,您可以使用绘制一个标记而不是一条线
plt.plot(Tx, 'bo', label='merge_sort')
线性 y 尺度
Tx = [0.0019073486328125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.plot(Tx, label='merge_sort')
plt.xlabel("n")
plt.ylabel('ms')
plt.legend(loc = "upper right")
plt.show()
对数 y 标度
Tx = [0.0019073486328125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.plot(Tx, label='merge_sort')
plt.xlabel("n")
plt.ylabel('ms')
plt.yscale('log')
plt.legend(loc = "upper right")
plt.show()
推荐阅读
- python - python- sys.argv CLI 参数的帮助程序列表
- filenotfoundexception - 自己导入 spring-boot-starter,开始打印错误日志 'Unable to read meta-data for class'
- python - 转换为日期的正确方法
- polymer - 在测试聚合物时无法读取未定义的属性“匹配”
- reactjs - 我们如何在 React 应用程序中配置适用于本地和生产环境的服务器 URL?
- sql - SQL Server - 过去 52 周到给定周的交易量的分配(总和)
- angular - 如何引用数组中的组件?
- java - 一个简单的问题,关于动态 Java,它可行吗?还是完全不可能?
- javascript - Javascript:计算两个日期之间的月数
- python - 尝试用 IronPython 做线性代数