欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

plot 双线

程序员文章站 2022-03-19 16:41:14
...
def tongBiLineView(request):
    area=request.GET['area']
    flag = request.GET['flag']
    plt.figure(figsize=(30, 20))
    plt.rcParams['savefig.dpi'] = 80
    plt.rcParams['figure.dpi'] = 80
    # 把x轴的刻度间隔设置为隔120一显示,并存在变量里
    x_major_locator = MultipleLocator(120)
    ax = plt.gca()
    ax.xaxis.set_major_locator(x_major_locator)
    # ----------
    plt.rcParams['font.sans-serif']=['SimHei']
    # -----------
    plt.title(area+'家庭宽带在线用户数趋势图',fontsize=50)
    # 设置y轴科学计数法乘10的四次方
    formatter = FuncFormatter(formatnum)
    ax.yaxis.set_major_formatter(formatter)
    #调整x,y轴的字大小
    plt.tick_params(labelsize=30)
    plt.xticks(rotation=30)
    # plt.yticks()
    # 根据时间
    # onedayagoforecast_time = Onedayagocount.objects.get(region=area).forecast_time
    # 日期转字符串
    nowDatetime = datetime.datetime.now()
    yesterdayDatetime=timeSubDay(nowDatetime)
    nowDatetimeStr=nowDatetime.strftime('%Y-%m-%d')
    nowDatetimeStr=nowDatetimeStr+" 00:00"
    yesterdayDatetimeStr=yesterdayDatetime.strftime('%Y-%m-%d')
    yesterdayDatetimeStr=yesterdayDatetimeStr+" 00:00"
    conn=mysqlconn()
    if flag=='1':
        # 今天
        # tablename = returntablename(region)
        cur = conn.cursor()
        sqlStr="SELECT DISTINCT create_time,counts from online_hz where object='"+area+"' and create_time>'"+nowDatetimeStr+"' ORDER BY create_time  ;"
        cur.execute(sqlStr)
        info = cur.fetchall()
        cur.close()
        if len(info)!=0:
            data = []
            alldataTime=list()
            for  todayCounts in info:
                data.append(todayCounts[1])
                alldataTime.append((todayCounts[0]).strftime('%H:%M'))
            plt.plot(alldataTime,data, 'b',linewidth=3,label=area+'当天在线用户数')
            # 当前值
            now_index = len(data) - 1
            # now_value =data[data.size-1] # min value index
            show_now = '[' + str(alldataTime[now_index]) + ' ' + str(data[now_index]) + ']'
            plt.plot(now_index, data[now_index], 'ks', lw=10,label=u'当前实际在线用户:' + show_now)
            plt.annotate(show_now, xytext=(now_index, data[now_index]), xy=(now_index, data[now_index]), fontsize=30)
        #############################################################
        # 昨天
        cur2 = conn.cursor()
        sqlStr2="SELECT DISTINCT create_time,counts from online_hz where object='"+area+"' and create_time>'"+yesterdayDatetimeStr+"'and create_time<'"+nowDatetimeStr+"'  ORDER BY create_time  ;"
        print(sqlStr2)
        cur2.execute(sqlStr2)
        info2 = cur2.fetchall()
        cur2.close()
        conn.close()
        # 昨天数据呈现
        if len(info2)!=0:
            yesterdaydata = []
            yesterdaydataTime=list()
            yesterdayNowPointCount=0
            for  yesterdayCounts in info2:
                yesterdaydata.append(yesterdayCounts[1])
                yesterdayTime=(yesterdayCounts[0]).strftime('%H:%M')
                yesterdaydataTime.append(yesterdayTime)
                if yesterdayTime==alldataTime[now_index]:
                    yesterdayNowPointCount=yesterdayCounts[1]
            plt.plot(yesterdaydataTime,yesterdaydata, 'r',linewidth=3,label=area+'昨天在线用户数')
            ######################################################
            # 当前值
            show_now = '[' + str(alldataTime[-1]) + ' ' + str(yesterdayNowPointCount) + ']'
            plt.plot(alldataTime[-1], int(yesterdayNowPointCount), 'gs', label=u'昨天此刻在线用户:' + show_now)
            plt.annotate(show_now, xytext=(alldataTime[-1], int(yesterdayNowPointCount)),
                         xy=(alldataTime[-1], int(yesterdayNowPointCount)), fontsize=30)
        # ========================================
        plt.legend(loc='upper left')
        plt.grid(linestyle='-')
        plt.savefig('templates\\plotpic\\onlinetongbiplot.png')
        plt.close()
        try:
            data = request.GET
            file_name = data.get("file_name")
            imagepath = os.path.join(settings.BASE_DIR, "templates\\plotpic\\onlinetongbiplot.png".format(file_name))  # 图片路径
            with open(imagepath, 'rb') as f:
                image_data = f.read()
            return HttpResponse(image_data, content_type="image/png")
            # result = base64.b64encode(image_data)
            # # result='data:image/jpeg;base64,%s'%s
            # return HttpResponse(result, content_type='image/jpeg')
        except Exception as e:
            print(e)
            return HttpResponse(str(e))
相关标签: plot python