gpt4 book ai didi

python - 为什么我的 matplotlib 不绘图?

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:34 26 4
gpt4 key购买 nike

这是我的代码:

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.

为什么没有显示线条?

附注我的输出:

>>> Trie tree took 0.0350674204940301 seconds
>>> Binary Search Tree took 0.028901715849401904 seconds
>>> AVL tree took 0.030507586039193013 seconds

最佳答案

我会这样走

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()

获取 enter image description here

关于python - 为什么我的 matplotlib 不绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com