zoukankan      html  css  js  c++  java
  • 用python绘制趋势图

    import matplotlib.pyplot as plt #plt用于显示图片
    import matplotlib.image as mping #mping用于读取图片
    import datetime as dt
    import matplotlib.dates as mdates
    from pylab import *
    
    
    def draw_trend_chart(dates,y):
        mpl.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体
        mpl.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题
        
        x = [dt.datetime.strptime(d,'%Y/%m/%d').date() for d in dates]
        #plt.figure(figsize=(8,8))
        plt.figure()
    
        #plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
        #plt.gca().xaxis.set_major_locator(mdates.DayLocator())
        #plt.plot(x,y,"r--",linewidth=2)
        plt.plot(x,y,"r",linewidth=1)
        #plt.gcf().autofmt_xdate()
        
        #plt.xlabel("DATE") #x轴标签
        plt.ylabel("WEIGHT") #y轴标签
        plt.title("MY HEALTH TRACKING")#标题
    
        plt.savefig("liuyang.png") #保存图片名称
    
        lena = mping.imread('liuyang.png')  #读取图片文件信息
        lena.shape #(512,512,3)
    
        plt.imshow(lena) #显示图片
        plt.axis('off') #不显示坐标轴
        plt.title("")
        plt.show() #显示
    
    def get_weight_data(filename):
        time = []
        weight = []
        fileContent=open(filename,"r")
        for eachline in fileContent:
            eachData = eachline.strip('
    ').split(",")
            if eachData[-1].strip() =='':
                continue
            else:
                time.append(eachData[0])
                weight.append(eachData[1])
        return [time, weight]
    
    data = get_weight_data("data.csv")
    draw_trend_chart(data[0],data[1])
    复制代码

  • 相关阅读:
    第一次项目总结
    8.16 CSS知识点7
    2016y9m22d 博文分享
    2016y9m8d
    2016y9m7d
    2016y9m6d
    2016y9m5d
    2016.9.2博文分享!
    2016y8m16d
    2016y8m15d
  • 原文地址:https://www.cnblogs.com/valorchang/p/11282325.html
Copyright © 2011-2022 走看看