2017-03-22 67 views
0

我想在網頁上顯示pandas df.plot.bar()的結果。我該怎麼做? 這就是我想要的。如何使用Django在網頁上顯示data_frame.plot.bar()的結果?

def graph(request): 
    fig = Figure() 
    ax = fig.add_subplot(111) 
    data_df = pd.read_csv("C:/Users/yadynesh/Final_Project/original.csv") 
    data_df = pd.DataFrame(data_df) 
    data_df.plot.bar(ax=ax) 
    canvas = FigureCanvas(fig) 
    response = HttpResponse(content_type = 'image/png') 
    canvas.print_png(response) 
    return response 

回答

0

這解決了我的問題

from django.views.generic import View 
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas 
from matplotlib.figure import Figure 
from django.http import HttpResponse 
import pandas as pd 
import seaborn 

def graph(request): 
    fig = Figure() 
    fig.set_size_inches(15, 6, forward=True) 

    ax = fig.add_subplot(111) 
    fig.subplots_adjust(bottom=0.35) 
    final_itemset = pd.Series.from_csv("C:/Users/abc/final_two_itemset.csv") 

    final_itemset.plot.bar(ax=ax) 

    canvas = FigureCanvas(fig) 
    response = HttpResponse(content_type = 'image/png') 
    canvas.print_png(response) 
    return response 
相關問題