我有多个函数,我输入一个数组或dict以及一个路径作为参数,该函数将一个数字保存到特定路径的路径。
尽量保持示例尽可能小,但这里有两个函数:
dict = {"a": {"Germany": 20006.0, "United Kingdom": 20016.571428571428}, "b": {"Chad": 13000.0, "South Africa": 3000000.0},"c":{"Chad": 200061.0, "South Africa": 3000000.0}
}
一个非常小的示例dict是,但实际的dict包含56000个值:
if __name__ == "__main__":
plt.close('all')
print "Starting pattern charting...\n"
countryChartPatterns(dict,'newPatternCountries.png'))
valueChartPatterns(dict,'newPatternValues.png'))
在我的剧本中,我打电话给:
import matplotlib.pyplot as plt
注意,我加载了Starting pattern charting...
。
在PyCharm中运行此脚本时,我在我的控制台中获得了joe = fulfill.receipt
joe.response_status = "Accepted"
joe.response_comment = "Your order was received by Yo Mama"
joe.save
,但这些函数需要超长时间来绘制。
我做错了什么?我应该使用直方图而不是条形图,因为这应该达到给出国家/值出现次数的相同目的吗?我可以以某种方式更改我的GUI后端吗?欢迎任何建议。
答案 0 :(得分:1)
这是我在上面的评论中提到的测试,结果是:
Elapsed pre-processing = 13.79 s
Elapsed plotting = 0.17 s
Pre-processing / plotting = 83.3654562565
测试脚本:
import matplotlib.pylab as plt
from collections import Counter
from operator import itemgetter
import time
def countryChartPatterns(dict,path):
# pre-processing -------------------
t0 = time.time()
seen_countries = Counter()
for data in dict.itervalues():
seen_countries += Counter(data.keys())
seen_countries = seen_countries.most_common()
yvals = map(itemgetter(1), seen_countries)
xvals = map(itemgetter(0), seen_countries)
dt1 = time.time() - t0
print("Elapsed pre-processing = {0:.2f} s".format(dt1))
t0 = time.time()
# plotting -------------------
plt.figure()
countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9,align='center')
plt.xticks(range(len(seen_countries)), xvals)
plt.title('Countries in Pattern Dataset')
plt.xlabel('Countries in Data')
plt.ylabel('Occurrences')
plt.tick_params(axis='both', which='major', labelsize=6)
plt.tick_params(axis='both', which='minor', labelsize=6)
plt.tight_layout()
plt.savefig(path)
plt.clf()
dt2 = time.time() - t0
print("Elapsed plotting = {0:.2f} s".format(dt2))
print("Pre-processing / plotting = {}".format(dt1/dt2))
if __name__ == "__main__":
import random as rd
import numpy as np
countries = ["United States of America", "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua & Deps", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan"]
def item():
return {rd.choice(countries): np.random.randint(1e3), rd.choice(countries): np.random.randint(1e3)}
dict = {}
for i in range(1000000):
dict[i] = item()
print("Starting pattern charting...")
countryChartPatterns(dict,'newPatternCountries.png')