Python matplotlib以日期为x轴作图代码实例

yipeiwu_com6年前Python基础

这篇文章主要介绍了Python matplotlib以日期为x轴作图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

效果图如下

代码如下

from datetime import datetime, date, timedelta
import matplotlib.pyplot as plt
import tushare as ts

plt.rcParams['font.sans-serif'] = ['SimHei'] #显示中文
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号


def get_riqi():
  count = 30
  i = 0
  today = date.today() #今天日期
  riqi_list=[]
  riqi_list.append(str(today))
  while(i<=count):
    i+=1
    riqi_list.append(str((today + timedelta(days=-i))))
  return riqi_list


def count(Data):
  data_list = []
  sum_list = []
  for data in Data:
    sum = 0
    list = []
    for jiage in data:
      sum+=jiage
      list.append(jiage)
    sum_list.append(round(sum,2))
    data_list.append(list)
  return sum_list,data_list

def get_juedui(Data):
  sum = 0
  data = []
  for i in Data[::-1]:
    sum += i
    data.append(sum)
  return data
technology = {
  '海康威视':'002415',
  '中兴通讯':'000063',
  '科大讯飞':'002230',
  '立讯精密':'002475',
  '蓝思科技':'300433',
  '歌尔股份':'002241'
}
store = [technology]
technology = []
def get_data(store,riqi_list):
  count = 0
  for Stock_pool in store:
    count +=1
    for stock in Stock_pool.values():
      data = ts.get_hist_data(stock,start=riqi_list[-1])
      if (count == 1):
        technology.append(data.loc[:, 'p_change'])
riqi_list = get_riqi()
get_data(store,riqi_list)
riqi_list = technology[0].index
rili = []
for riqi in riqi_list:
  rili.append(riqi[5:])
sum2,data2 = count(technology)
riqi_list = [datetime.strptime(d, '%m-%d').date() for d in rili]
count = 0
for i in data2:
  count+=1
  data = get_juedui(i)
  if(count==1):
   plt.plot(rili[::-1], data,label = '海康威视' )
  if(count==2):
   plt.plot(rili[::-1], data,label = '中兴通讯' )
  if(count==3):
   plt.plot(rili[::-1], data,label = '科大讯飞' )
  if(count==4):
   plt.plot(rili[::-1], data,label = '立讯精密' )
  if (count == 5):
   plt.plot(rili[::-1], data, label='蓝思科技')
  if (count == 6):
   plt.plot(rili[::-1], data, label='歌尔股份')
plt.legend()
plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

TensorFlow实现AutoEncoder自编码器

TensorFlow实现AutoEncoder自编码器

一、概述 AutoEncoder大致是一个将数据的高维特征进行压缩降维编码,再经过相反的解码过程的一种学习方法。学习过程中通过解码得到的最终结果与原数据进行比较,通过修正权重偏置参数降低...

python Pillow图像处理方法汇总

这篇文章主要介绍了python Pillow图像处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Pillow中文文档:ht...

Django框架中处理URLconf中特定的URL的方法

有时你有一个模式来处理在你的URLconf中的一系列URL,但是有时候需要特别处理其中的某个URL。 在这种情况下,要使用将URLconf中把特殊情况放在首位的线性处理方式 。 比方说,...

python 遍历字符串(含汉字)实例详解

python 遍历字符串(含汉字)实例详解 s = "中国china" for j in s: print j 首先一个,你这个'a'是什么编码?可能不是你所想的gbk &...

Python模块文件结构代码详解

本文研究的主要是Python模块文件结构的相关内容,具体如下。 Python文件结构 文件结构(范例全文) #/usr/bin/env python "this is a...