Rumah > Artikel > pembangunan bahagian belakang > Siri praktikal Python |
with open('data.txt') as f:
data = f.read()
datas = re.findall('({.*?})',data)
re.findall('\'(.*)\'',datas[0])[0]
re.findall('(\d+(\.\d+)?)',datas[0])
for i in range(0,len(datas_tmp),4): datas_f.append(float(datas_tmp[i+3][0])) dates_f.append(f'{datas_tmp[i][0]}-{datas_tmp[i+1][0]}-{datas_tmp[i+2][0]}')
?️ Kod lengkap?
# 处理数据 with open('data.txt') as f: data = f.read() datas = re.findall('({.*?})',data) names = [] dates_result = [] datas_result = [] for idx,dd in enumerate(datas): datas_f = [] dates_f = [] name = re.findall('\'(.*)\'',dd)[0] names.append(name) datas_tmp = re.findall('(\d+(\.\d+)?)',dd) for i in range(0,len(datas_tmp),4): datas_f.append(float(datas_tmp[i+3][0])) dates_f.append(f'{datas_tmp[i][0]}-{datas_tmp[i+1][0]}-{datas_tmp[i+2][0]}') datas_result.append(datas_f) dates_result.append(dates_f)
?️? 6. 绘图
绘图部分直接用matplotlib的plot循环绘制即可,代码如下:
# 绘图 plt.figure(figsize=(20, 10), dpi=100) for i in range(len(names)): plt.plot(dates_result[i], datas_result[i], label=names[i]) ax = plt.gca() ax.xaxis.set_major_locator(ticker.MultipleLocator(20)) plt.ylabel("Ratings(%)", fontdict={'size': 16}) plt.title("TIOBE Programming Community Index", fontdict={'size': 20}) plt.legend(loc='best') plt.grid(True, linestyle='--', alpha=0.5) plt.show()
Atas ialah kandungan terperinci Siri praktikal Python |. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!