python绘制趋势图的示例

时间:2022-07-01 18:45:41
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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绘制趋势图的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/liuyang92/p/7466600.html