SaveFig(graph_filename) VS canvas.print_png(graph_filename)

56 views
Skip to first unread message

Thames Khi

unread,
May 10, 2017, 12:03:51 PM5/10/17
to Django users
I have a small app which generates a graph and saves it to a file. The file is then displayed using a reference. My question is, is it better to use matplotlib.pyplot SaveFig function or  matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas function? Here is an extract of my code:

Views:


from django.shortcuts import render
from . import graphs
from django.conf import settings as djangoSettings
import os.path

def graph(request):

from datetime import datetime, timedelta

datetoday,datef = interDateFunction()
graph_filename = (djangoSettings.STATICFILES_DIRS[0] + 'graph_' + datetoday + '.png')
filename = ('graph_' + datetoday + '.png')

if not os.path.exists(graph_filename):
graphs.genGraph(graph_filename)
args = {'Value':'Generated','DateF':datef,'Filename':filename}
return render(request, 'graphs/home.html', args)
else:
args = {'Value':'FromCache','DateF':datef,'Filename':filename}
return render(request, 'graphs/home.html', args)



The view references code in the graphs.py:

def genGraph(graph_filename):

dtStart = (dt.datetime.strptime("2015-01-01", '%Y-%m-%d'))
datast = data.get_data_google('TSCO.L', dtStart)['Close']
fig,ax = plt.subplots()
ax.plot(datast)
fig.savefig(graph_filename)
plt.close(fig)
return True


I have seen examples where they use matplotlib.backends.backend_agg function FigureCanvasAgg. Which is better to use with django?

Thanks,

Khi.
  






Melvyn Sopacua

unread,
May 10, 2017, 5:06:25 PM5/10/17
to django...@googlegroups.com

On Wednesday 10 May 2017 09:03:51 Thames Khi wrote:

> I have a small app which generates a graph and saves it to a file. The

> file is then displayed using a reference.

 

Consider saving some CPU cycles:

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_store

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages