在Matplotlib和django看起来奇怪的情节

时间:2013-01-15 20:04:36

标签: python django matplotlib

我看到来自matplotlib的一些奇怪的结果,我使用了很多股票示例将它添加到Django;

def graph(request):
if request.user.is_authenticated():
    user = request.user.first_name
else:
    return redirect('/login')


form = GetGraphData(request.POST or None)
if form.is_valid():

    os.environ['MPLCONFIGDIR'] = "/tmp"
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter



    from_date = form.cleaned_data['chosen_from_date']
    to_date = form.cleaned_data['chosen_to_date']
    graph_property = form.cleaned_data['chosen_property']
    rack = form.cleaned_data['chosen_rack']
    query = "select %s,reading_date from readings where reading_date" \
            " between '%s' and '%s' and pdu_location like '%s%%'" \
            %(graph_property,from_date, to_date, rack)



    results = db.db_query(query)

    if results[0]: 
        fig=Figure()
        fig.clear()
        ax=fig.add_subplot(111)
        x=[]
        for i in results[0]:
            x.append(i[1])
        y=[]
        for i in results[0]:
            y.append(i[0])

        ax.plot_date(x, y, '-')
        ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
        ax.set_title(rack)
        ax.set_xlabel("Date")
        ax.set_ylabel(graph_property)
        fig.autofmt_xdate()
        canvas=FigureCanvas(fig)
        response=HttpResponse(content_type='image/png')
        canvas.print_png(response)
        return response
    else:
        error_message = 'There were no results for that query, check' \
                        ' your dates and try again.' 
        return render_to_response('generic_message.html'
                           ,{'message':error_message,'title':'No results'})




template_dict = {'username':user, 'form':form,'view':True}
return render_to_response('get_circuit.html',template_dict)

缩进是正确的,它只是在上面奇怪地贴上, 在x轴上,我们最终得到一个日期时间对象列表,在y轴上我们最终得到一个浮点列表。

我得到下面的图表; enter image description here

你可以看到图表的左侧非常奇怪。 有96个数据点作为此查询的一部分返回,我还注意到,当y轴上有超过1000个数据点时,它会变得更加疯狂。

有人会有任何建议来解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

看起来你的观点并没有线性绘制。我相信按日期排序应该解决它

order by reading_date ASC