ImportMatplotlib.pyplot as Plt#plt for displaying picturesImportMatplotlib.image as Mping#mping for reading picturesImportdatetime as DTImportMatplotlib.dates as Mdates fromPylabImport*defDraw_trend_chart (dates,y): mpl.rcparams['Font.sans-serif'] = ['Simhei']#Specify a default fontmpl.rcparams['Axes.unicode_minus'] = False#fix an issue where saving image is minus '-' displayed as a blockx= [Dt.datetime.strptime (d,'%y/%m/%d'). Date () forDinchDates]#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-axis labelsPlt.title ("MY Health TRACKING")#titlePlt.savefig ("Liuyang.png")#Save Picture NameLena= Mping.imread ('Liuyang.png')#Read picture file informationLena.shape#(512,512,3)Plt.imshow (Lena)#Show PicturesPlt.axis ('off')#do not display axesPlt.title ("") plt.show ()#Showdefget_weight_data (filename): time=[] Weight=[] filecontent=open (filename,"R") forEachlineinchFilecontent:eachdata= Eachline.strip ('\ n'). Split (",") ifEachdata[-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])
Plot a trend chart with Python