这是我的代码:
from Trees import *
from timeit import default_timer as timer
import re
import matplotlib.pyplot as plt
import pickle
def read_file(file):
words = []
for line in wordFile:
for word in line.split():
words.append(re.sub("[^a-z]", "", word.lower()))
return words
def pickle_object(object, fileName):
with open(fileName, "wb") as output:
pickle.dump(object, output, pickle.HIGHEST_PROTOCOL)
wordFile = open("texts/words1.txt", "r")
wordList = read_file(wordFile)
baseTrie = Trie()
trie2 = Trie()
bst = BinarySearchTree()
avl = AVLTree()
for item in wordList:
baseTrie.add(item)
trieList = baseTrie.list("", [])
for word in trieList:
bst.put(word, None)
avl.put(word, None)
trie2.add(word)
start = timer()
testTrieList = trie2.list("", [])
end = timer()
trieTime = end - start
start = timer()
bst.inorder()
end = timer()
bstTime = end - start
start = timer()
avl.inorder()
end = timer()
avlTime = end - start
#pickle_object(baseTrie, "Pickled Trees/trie.pkl")
print("Trie tree took %s seconds" % (trieTime))
print("Binary Search Tree took %s seconds" % (bstTime))
print("AVL tree took %s seconds" % (avlTime))
plt.plot(100, trieTime, label="Trie")
plt.plot(bstTime, label="BST")
plt.plot(avlTime, label="AVL")
plt.legend()
plt.show()
我想显示每个秒数花费的时间(以秒为单位)。但是,每当我尝试运行该文件时,结果如下: My plot.
为什么线条没有显示?
P.S。我的输出:
>>> Trie tree took 0.0350674204940301 seconds
>>> Binary Search Tree took 0.028901715849401904 seconds
>>> AVL tree took 0.030507586039193013 seconds
答案 0 :(得分:1)
我会这样做
abscissae = (10, 20, 30)
labels = ('Trie', 'BST', 'AVL')
plt.scatter(abscissae, (trieTime, bstTime, avlTime))
### E D I T — we want to compare the different timings, not the differences
### with respect to an arbitrary baseline!
plt.ylim(ymin=0)
plt.xticks(abscissae, labels)
plt.grid()
plt.show()