Django - 如何最好地集成matplotlib

问题描述 投票:1回答:1

我正在尝试使用matplotlib和django显示图形。我已经在堆栈上阅读了很多问题,但仍然不明白在Django中使用matplotlib的最佳方法是什么。

情况:

我有一个包含大量数据的模型。在views.py中,我有一个简单的表单,它捕获一些数据并查询模型并返回数据的子集。这是相关的views.py部分:

def getinput(request):
     if request.method == 'POST':
         form = get_data(request.POST)

         if form.is_valid():
             down = form.cleaned_data['get_down']
             ytg = form.cleaned_data['get_ytg']
             yfog = form.cleaned_data['get_yfog']
             map_data = next_play.objects.filter(last_dwn__exact=down, last_yfog__exact=yfog, last_ytg__exact=ytg)
             context = {'form': form, 'query_data': map_data}
             return render(request, 'play_outcomes/output.html', context)
     else:
         form = get_data()


     return render(request, 'play_outcomes/getinput.html', {'form': form})

当我到达play_outcomes/getinput并输入dwn ytg yfog模板然后输出大量数据。

这是我要绘制的数据,即map_data中的数据。

题:

如何将matplotlib集成到此?我是否将views.py中的matplotlib代码集成,我应该在单独的python模块中进行设置吗?大概我需要创建一个png文件然后显示?

python django matplotlib
1个回答
2
投票

图是一个视图,因此视图是一个好地方。另一方面,matplot lib可能很冗长,你想设置各种常量,这样每个图形看起来都有类似的风格。出于这个原因,我建议将所有图形代码移动到新文件中。

一般来说,我避免在Django中创建图形。只是一个“离线”批处理,创建一些统计图表,或者我会尝试使用d3.js,将数字生成卸载到客户端(但是他们接收数据集)。还有一些混合变体,我从未尝试过,例如http://subsetlab.com/super-fund-performance-and-fees.html(检查第二部分:“怎么做”)。

© www.soinside.com 2019 - 2024. All rights reserved.