使用Django展示pandas图表​

使用Django展示pandas图表​

views.py中

from django.shortcuts import render

# Create your views here.
import io
import base64
import pandas as pd
import matplotlib.pyplot as plt

# 防止出现警告提醒
import matplotlib as mpl
mpl.use('Agg') 

def index(request):
    img = io.BytesIO()
    book2 = pd.read_excel('D:\\pandas_learn\\book2.xlsx')
    book2[2017].plot.pie()
    plt.savefig(img, format='png')
    plot_url = base64.b64encode(img.getvalue()).decode()
    data={
        'plot_url':'data:image/png;base64,'+plot_url
    }
    return render(request, 'plot.html',data)

plot.html中

<!DOCTYPE html>
<html>
<body>
    <img src="{{ plot_url }}">
    <p>{{ text }}</p>
</body>
</html>


版权声明:本文为qq_42164596原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。