django matplotlib 如何将图返回给前端

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 }}">
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值