django matplotlib 如何将图返回给前端
django plt如何将图返回给前端
django matplotlib图去除白边返回给前端
django matplotlib画图去除白边并返回给前端
django可视化
django python 后端画图返回给前端
后端
plt.figure(figsize=(len(display_list)*4,len(display_list)*10))
for i in range(len(display_list)):
x=[]
bike_y=[]
car_y=[]
scooter_y=[]
for j in range(len(display_list[i])):
time_local = time.localtime(display_list[i][j][3])
bike_y.append(display_list[i][j][0])
car_y.append(display_list[i][j][1])
scooter_y.append(display_list[i][j][2])
if period =="minutes":
dt = time.strftime("%H:%M",time_local)
x.append(dt)
elif period =="hours":
dt = time.strftime("%d %H",time_local)
x.append(dt)
elif period == "days":
dt = time.strftime("%m-%d",time_local)
x.append(dt)
plt.subplot(len(display_list),1,i+1)
plt.plot(x,bike_y, label='bike')
plt.plot(x,car_y, "--",label='car')
plt.plot(x,scooter_y,":", label='scooter')
plt.legend()
plt.title(station_name_list[i][0])
if period =="minutes":
plt.xlabel("hours minutes")
elif period =="hours":
plt.xlabel("days hours")
elif period == "days":
plt.xlabel("months days")
plt.ylabel("numbers")
sio = io.BytesIO()
plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
data = base64.encodebytes(sio.getvalue()).decode()
src = 'data:image/png;base64,' + data
context={
'img':src,
}
return render(request, "manager.html",context)
前端
<img src="{{ img }}">