天天看點

python怎麼用matplotlib_python – 如何在Django中使用Matplotlib?

從網際網路的一些例子,我做了下面的測試代碼.有用!

…但是如果我重新加載頁面,餅圖将自己繪制相同的圖像.每次重新加載頁面時,某些部分會變暗.當我重新啟動開發伺服器,它被重置.在Django中如何使用Matplotlib正确繪制?它看起來像記得一些圖紙…

來源views.py(讓urls.py連結到它):

from pylab import figure, axes, pie, title

from matplotlib.backends.backend_agg import FigureCanvasAgg

def test_matplotlib(request):

f = figure(1, figsize=(6,6))

ax = axes([0.1, 0.1, 0.8, 0.8])

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'

fracs = [15,30,45, 10]

explode=(0, 0.05, 0, 0)

pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)

title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})

canvas = FigureCanvasAgg(f)

response = HttpResponse(content_type='image/png')

canvas.print_png(response)

return response

我使用的是Django 1.0.1和Python 2.6.2(Linux).