【之前很少用python画图,看到有年轻人在用,很方便,稍微学习下】
折线图文本数据 linechart-data
(日期,曝光数据1,曝光数据2)
20171009,258649,28702
20171010,213938,24891
20171011,257165,28111
20171012,346909,39771
20171013,414327,44051
20171014,457955,46977
20171029,639724,64127
20171030,665387,63682
20171031,656545,64932
20171101,853414,83532
20171102,609153,60805
20171103,663003,65402
20171104,719139,64303
python代码(复制粘贴的时候请注意格式,因为我从pycharm里把代码拷贝出来的时候格式发生了变化)
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
import numpyas np
import matplotlib.dates as mdates
from datetime import datetime
# xs, y1, y2 = np.loadtxt('linechart-data', dtype=str, delimiter=',',usecols=(0, 1, 2), unpack=True)
xs, y1 ,y2= np.loadtxt('linechart-data',dtype=str,delimiter=',',usecols=(0,1,2),unpack=True)
# x = range(0, len(xs))
x= [datetime.strptime(d,'%Y%m%d').date() for d in xs]
plt.plot(x, y1,label=u'预测',color='red')
plt.plot(x, y2,label=u'预测',color='green')
# 配置横坐标
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y%m%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator()) # 日期显示粒度为天,可以换成周、月、年等
plt.gcf().autofmt_xdate() #日期自动排列
plt.grid(b=True,which='major',axis='y') #显示网格线
plt.xlabel('date')
plt.ylabel('pv')
plt.title(u'双折线图')
plt.legend()
plt.show()