-- coding: UTF-8 --
#结合shelve和mathplotlib画图
#Step1,先生成随机数
import shelve
import random
#Step1.1 先把Python小王子的数据生成出来
name=“我是Python小王子”
data_dict={}
for key in range(1,100):
data_dict[key]=random.randint(0,100)
data_dict_with_name={}
data_dict_with_name[name] = data_dict
print(data_dict_with_name[name])
print(data_dict_with_name)
#Step1.2 再把Java小王子的数据生成出来
name=“我是Java小王子”
data_dict={}
for key in range(1,100):
data_dict[key]=random.randint(0,50)
data_dict_with_name[name] = data_dict
print(data_dict_with_name[name])
print(data_dict_with_name)
#Step2 把各位小王子们的数据写到数据中
s = shelve.open(“E:\tmp\20190914.db”,writeback=True)
s[“princes”] = data_dict_with_name
#Step2 把各位小王子们的数据读出来,然后显示
import matplotlib.pyplot as plt
import numpy as np
#Step2.1 先把小王子各自的数据字典读出来
for k,v in s[“princes”].items():
data_dict_key = k
data_dict_value = v
#Step2.2 再把小王子的数据插到plot中去
keys = data_dict_value.keys()
values = data_dict_value.values()
plt.plot(keys,values,label=data_dict_key)
plt.xlabel(‘Name’,fontsize=14)
plt.ylabel(‘Random VAlue’,fontsize=14)
plt.title(‘We are the Princes’,fontsize=18)
plt.show()