0

我从 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 轴上,我们最终得到一个浮点数列表。

我得到下面的图表; 在此处输入图像描述

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

有人有什么建议来解决这个问题吗?

4

1 回答 1

1

看起来你的点不是线性绘制的。我相信按日期订购应该可以解决它

order by reading_date ASC

于 2013-01-15T20:18:46.340 回答