首页 > 解决方案 > 使用折线图而不是条形图显示排序性能

问题描述

我正在尝试分析折线图中的合并排序排序性能,但仍然显示条形图,我该如何实现折线图中的图形?我正在使用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()

标签: pythonsortingmatplotlibgraph

解决方案


问题如下所示:

  • 您的Tx. 其余所有值均为零,未定义日志。所以事情在线性尺度上看起来不错,但是当你在对数 y 尺度上绘制事物时,唯一的峰值出现在非零值上,因为它与-inflog(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()

在此处输入图像描述


推荐阅读