当前位置:首页 >> 脚本专栏

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('\n').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])

python绘制趋势图的示例

以上就是python绘制趋势图的示例的详细内容,更多关于python绘制趋势图的资料请关注其它相关文章!