1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python numpy+pandas+matplotlib学习笔记

Python numpy+pandas+matplotlib学习笔记

时间:2021-01-10 05:56:03

相关推荐

Python numpy+pandas+matplotlib学习笔记

Python numpy+pandas+matplotlib

本文是根据b站路飞学城Python数据分析全套教程的学习视频整理归纳的学习文档,主要目的是方便自己进行查阅,详细的还得去b站看原视频。另外有很多文字都是根据我自己的理解写的,比较小白,欢迎指正。

文章目录

Python numpy+pandas+matplotlibNumpy 使用ipython编译array 创建array索引和切片array布尔型索引array花式索引array通用函数统计方法和随机数生成 Pandaspandas介绍Seriesseries-一维数据对象series整数索引问题series数据对齐series缺失值处理series小结 DataFrameDataFrame的创建DataFrame常用属性DataFrame索引和切片DataFrame-数据对齐与缺失数据 pandas常用函数时间对象时间对象处理时间对象生成时间序列 文件处理文件读取文件写入 Matplotlib 使用jupyter编译matplotlib介绍plot函数周边pandas与Matplotlibmatplotlib画布与子图matplotlib柱状图和饼图matplotlib K线图Tushare-金融数据接包口股票分析作业双均线分析作业

Numpy 使用ipython编译

array 创建

import numpy as npnp.array([1,2,3])>>>array([1,2,3])np.array([0]*10)>>>array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])np.zeros(10) # 此时该数组的dtype为float64 ,需转换为int才能正常输出0,后面的ones()同理>>>array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])np.zeros(10, dtype='int')>>>array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])np.ones(10, dtype='int')>>>array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])np.arange(起始数,数字个数,步长)np.linspace(起始数,最终数,数字个数) # 最后得出的数组各数之间步长相同

array索引和切片

import numpy as npa = np.arange(10)a>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])a+1>>>array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])a*3>>>array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])b = np.arange(10,20)>>>array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])a+b>>> array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])a>b>>>array([False, False, False, False, False, False, False, False, False,False], dtype=bool)

# 一维数组检索a = np.arange(10)a[0]>>>0# 二维数组检索np.arange(15)>>>array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10a, 11, 12, 13, 14])a = np.arange(15).reshape((3,5)) # 一维数组转二维数组>>>array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]])a[0][0]>>>0a[0,0]>>>0 # 两种写法

In [17]: aOut[17]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])In [18]: a[0:4]Out[18]: array([0, 1, 2, 3])In [19]: a[4:]Out[19]: array([4, 5, 6, 7, 8, 9])In [20]: a[:4]Out[20]: array([0, 1, 2, 3]) # 一维数组的三种切片方式In [21]: b = list(range(10))In [22]: bOut[22]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]In [23]: c=a[0:4]In [24]: d = b[0:4]In [25]: c[0] = 20In [26]: d[0] = 20In [27]: cOut[27]: array([20, 1, 2, 3])In [28]: dOut[28]: [20, 1, 2, 3]# 二维数组a = np.arange(15).reshape((3,5))a>>>array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14]])a[0:2,0:2]>>>array([[0, 1],[5, 6]])a[1:,2:4] # 前一个代表行,后一个代表列>>>array([[ 7, 8],[12, 13]])

array布尔型索引

Q1:给一个数组,选出数组中所有大于5的数

A1:a[a>5]

Q2: 给一个数组,选出数组中所有大于5的偶数

A2: a[(a>5) & (a%2==0)]

Q3: 给一个数组,选出数组中所有大于5的数和偶数

A3: a[(a>5) | (a%2==0)]

a = [random.randint(0,10) for i in range(20)]a>>>[3, 8, 7, 7, 2, 3, 7, 8, 4, 2, 0, 0, 6, 3, 9, 0, 1, 7, 6, 7]a = np.array(a) # 将a转化为数组list(filter(lambda x:x>5,a))>>>[8, 7, 7, 7, 8, 6, 9, 7, 6, 7] # 原方法a[a>5] # 选出数组中所有大于5的数>>>array([8, 7, 7, 7, 8, 6, 9, 7, 6, 7])a>5 # 判断数组的每一位是否大于5>>>array([False, True, True, True, False, False, True, True, False, False, False, False, True, False, True, False, False, True, True, True])a = np.arange(4)a>>>array([0,1,2,3])a[[True , False, True, False]]>>>array([0, 2]) # 对应位的数字不显示# Q1:给一个数组,选出数组中所有大于5的偶数a = [random.randint(0,10) for i in range(20)]a>>>[10, 9, 0, 10, 6, 8, 0, 5, 7, 3, 10, 3, 10, 1, 9, 5, 9, 5, 8, 4]a = np.array(a) # 将a转化为数组# 错误:a[a>5][a%2==0]b = a[a>5]b>>>array([10, 9, 10, 6, 8, 7, 10, 10, 9, 9, 8])b = b[b%2==0]b>>>array([10, 10, 6, 8, 10, 10, 8]) # 第一种方法a[(a>5) & (a%2==0)]>>>array([10, 10, 6, 8, 10, 10, 8]) # 第二种方法# Q2:筛选出数组中大于5的数和偶数a[(a>5) | (a%2==0)]>>>array([10, 9, 0, 10, 6, 8, 0, 7, 10, 10, 9, 9, 8, 4])

array花式索引

Q1:对于一个数组,选出其第1,3,4,6,7个元素,组成新的二维数组。

A1:a[[1,3,4,6,7]]

Q2:对一个二维数组,选出其第一列和第三列,组成新的二维数组。

A2:a[:,[1,3]]

a = np.arange(20)a>>>array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])# Q1a[[1,3,4,6,7]] >>>array([1, 3, 4, 6, 7])# Q2a = np.arange(20).reshape(4,5) # 转为二维数组a>>>array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19]])a[:,[1,3]]>>>array([[ 1, 3],[ 6, 8],[11, 13],[16, 18]])# 将6,8,16,18单独组成一个二维数组a[[1,3],:][:,[1,3]]>>>array([[ 6, 8],[16, 18]])

array通用函数

通用函数:能同时对数组中所有元素进行运算的函数常见通用函数: 一元函数:abs,sqrt,exp,log,ceil,floor,rint,trunc,modf,isnan,isinf,cos,sin,tan二元函数:add,substract,multiply,divide,power,mod,maximum,mininum

补充-浮点数特殊值

nan(Not a Number): 不等于任何浮点数(nan != nan)nif(infinity): 比任何浮点数都大,趋于无限值NumPy中创建特殊值: np.nan np.inf在数据分析中,nan常被用作表示数据缺失值

a = np.arange(-5,5)a>>>array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])np.abs(a) # 可以直接写abs(a)>>>array([5, 4, 3, 2, 1, 0, 1, 2, 3, 4])np.sqrt(a)>>>array([nan, nan, nan, nan, nan, 0. , 1. , 1.41421356, 1.73205081, 2. ])a = np.arange(-5.5,5.5)a>>> array([-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5])np.ceil(a) # 向上取整,例1.2取2,-2.3取-3>>>array([-5., -4., -3., -2., -1., -0., 1., 2., 3., 4., 5.])np.floor(a) # 向下取整,例1.2取1,-2.3取-2>>>array([-6., -5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])np.round(a) # 四舍五入>>>array([-6., -4., -4., -2., -2., -0., 0., 2., 2., 4., 4.])np.trunc(a) # 向0取整,>>>array([-5., -4., -3., -2., -1., -0., 0., 1., 2., 3., 4.])np.rint(a) # 同roundnp.modf(a) # 将小数与整数部分分开>>>(array([-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5]),array([-5., -4., -3., -2., -1., -0., 0., 1., 2., 3., 4.]))a = np.arange(0,5)>>> array([0, 1, 2, 3, 4])b = a/ab>>>array([nan, 1., 1., 1., 1.])np.isnan(b) # 判断数组内是否含有nan>>>array([ True, False, False, False, False])np.maximum(a,b) # 取出每一位最大的数字>>>array([nan, 1., 2., 3., 4.]) # nan无法进行比较np.minimum(a,b) # 取出每一位最小的数字>>>array([nan, 1., 1., 1., 1.])

统计方法和随机数生成

a = array([0, 1, 2, 3, 4])a.sum() # 求和>>>10a.mean() # 求平均值>>>2.0a.var() # 求方差,方差:每一位数减平均数后平方的和再除以数字的个数>>>2.0a.std() # 求标准差,标准差及方差的开根>>>1.4142135623730951a.argmax()# 求最大值索引,返回最大值的下标>>>4

随机数生成

随机数函数在np.random子包内 rand 给定形状产生随机数组(0到1之间的数)randint 给定形状产生随机整数choice 给定形状产生随机选择shuffle 与random.shuffle相同uniform 给定形状产生随机数组

np.random.randint(0,10,10) # 随机生成10个0到10的整数,第三个参数为输出结果的shape,例如第三个参数为(3,5)将会生成三行五列的随机0到10的整数>>>array([8, 6, 9, 7, 1, 1, 6, 3, 9, 0])array([0, 1, 2, 3, 4])np.random.shuffle(a)# 打乱数组a>>>array([4, 0, 3, 2, 1])np.random.rand(10)# 随机生成0到1之间的数组,括号内为输出结果shape>>>array([0.59299055, 0.62454554, 0.22663945, 0.19710862, 0.40294187, 0.69161135, 0.13875322, 0.62038921, 0.96111296, 0.32484364])np.random.choice(a,(2,1))# 在数组中随机选择输出,第二个参数为shape>>>array([[3],[4]])np.random.uniform(2.0,4.0,5)# 生成范围内随机数组,第三个shape>>>array([3.6005868 , 2.54784429, 2.62126056, 2.45776224, 2.68572162])

Pandas

pandas介绍

pandas是一个强大的Python数据分析的工具包,是基于Numpy构建的pandas的主要功能: 具备对其功能的数据结构DataFrame、Series集成时间序列功能提供丰富的数学运算和操作灵活处理缺失数据

Series

series-一维数据对象

series是一种类似于一维数组的对象,由一组数据和一组与之相关的数据标签(索引)组成。

创建方式:pd.Series([4,7,-5,3])

pd.Series([4,7,-5,3],index=[‘a’,‘b’,‘c’,‘d’])

pd.Series({‘a’:1,‘b’:2})

pd.Series(0,index=[‘a’,‘b’,‘c’,‘d’])

获取值数组和索引数组:values属性和index属性

Series比较像列表(数组)和字典的结合体

series-使用特性

import pandas as pdpd.Series([2,3,4,5])>>>0 21 32 43 5dtype: int64pd.Series([4,7,-5,3],index=['a','b','c','d']) # 自定义索引>>>a 4b 7c -5d 3dtype: int64a = array([4, 0, 3, 2, 1])pd.Series(a)# 对数组>>>0 41 02 33 24 1dtype: int32

series整数索引问题

整数索引的pandas对象往往会使新手抓狂

例:

​ sr = pd.Series(np.arange(4.))

​ sr[-1]

如果索引是整数类型,则根据整数进行下标获取值时总是面向标签。

解决方法:loc属性(将索引解释为标签)和iloc属性(将索引解释为下标)

sr = pd.Series(np.arange(20))>>>00112233...16 1617 1718 1819 19dtype: int32sr2 = sr[10:].copy()sr2>>>10 1011 11...17 1718 1819 19dtype: int32sr2[10]# 此时索引默认为标签,不推荐>>>10sr2.loc[10]# 同sr2[10],loc为属性,iloc相反>>>10sr2.iloc[9]# 将索引解释为下标>>>19

series数据对齐

例: sr1 = pd.Series([12,23,34],index=[‘c’, ‘a’, ‘d’])sr2 = pd.Series([11,20,10],index=[‘d’, ‘c’, ‘a’])sr1+sr2

会自动根据标签对齐,所以sr1+sr2 最终结果为

>>>a 33c 32d 45dtype: int64

如果数组sr2中有sr1中不存在的标签,则最后sr1+sr2的结果中该标签的值为NaN

(反之同理)

例:

sr1 = pd.Series([12,23,34],index=['c', 'a', 'd'])sr2 = pd.Series([11,20,10,15],index=['d', 'c', 'a','b'])sr1+sr2>>>a 33.0bNaNc 32.0d 45.0dtype: float64

当sr1 = pd.Series([12,23,34],index=[‘c’, ‘a’, ‘d’]),

sr2 = pd.Series([11,20,10],index=[‘b’, ‘c’, ‘a’])时,sr1+sr2的结果中b标签和d标签的值分别为NaN,那如何使结果在索引’b’处的值为11,索引’d’处的值为34呢?

运用算术方法:add,sub,div,mul

sr1.add(sr2,fill_value=0)

series缺失值处理

例如 sr

sr>>>a 33.0bNaNc 32.0dNaNdtype: float64

在sr中索引’b’和’d’的值都是NaN,缺失状态。对这些缺失值的处理可以有以下几种方法。

1.删除

sr.isnull()# 首先判断是否有缺失值>>>a FalsebTruec FalsedTruedtype: bool# sr.notnull()返回结果与isnull()相反sr[sr.notnull()]# 可以删除缺失值>>>a 33.0c 32.0dtype: float64sr.dropna()# 可以删除缺失值

2.填充

sr.fillna(0)# 将缺失值填充为'0'>>>a 33.0b0.0c 32.0d0.0dtype: float64sr.fillna(sr.mean())# 将缺失值填充为平均值>>>a 33.0b 32.5c 32.0d 32.5dtype: float64

series小结

series数组+字典的集合体整数索引loc和iloc数据对齐nan缺失数据处理dropna fillna

DataFrame

DataFrame的创建

DataFrame-二维数据对象

DataFrame是一个表格型的数据结构,含有一组有序的列。DataFrame可以被看做是由Series组成的字典,并且共用一个索引。

创建方式:

​ pd.DataFrame({‘one’:[1,2,3,4], ‘two’:[4,3,2,1]})

​ pd.DataFrame({‘one’:pd.Series([1,2,3],index=[‘a’, ‘b’, ‘c’]), ‘two’:pd.Series([1,2,3,4],index=[‘b’, ‘a’, ‘c’, ‘d’])})

​ …

csv文件读取与写入:

​ df.read_csv(‘filename.csv’)

​ df.to_csv()

pd.DataFrame({'one':[1,2,3,4], 'two':[4,3,2,1]})>>>one two0 1 41 2 32 3 23 4 1pd.DataFrame({'one':pd.Series([1,2,3],index=['a', 'b', 'c']), 'two':pd.Series([1,2,3,4],index=['b', 'a', 'c', 'd'])})>>>one twoa 1.0 2b 2.0 1c 3.0 3d NaN 4

DataFrame常用属性

df>>>one two0 1 41 2 32 3 23 4 1df.index>>>RangeIndex(start=0, stop=4, step=1)df.values>>>array([[1, 4],[2, 3],[3, 2],[4, 1]], dtype=int64)df.T# 行列交换>>>0 1 2 3one 1 2 3 4two 4 3 2 1df.describe()# 对每一列进行统计>>>one twocount 4.000000 4.000000mean 2.500000 2.500000std 1.290994 1.290994min 1.000000 1.00000025% 1.750000 1.75000050% 2.500000 2.50000075% 3.250000 3.250000max 4.000000 4.000000

DataFrame索引和切片

DataFrame是一个二维数据类型,所以由行索引和列索引。DataFrame同样可以通过标签和位置两种方法进行索引和切片loc属性和iloc属性 使用方法:逗号隔开,前面是行索引,后面是列索引行/列索引部分可以是常规索引、切片、布尔值索引、花式索引任意搭配

DataFrame-数据对齐与缺失数据

DataFrame对象在运算时,同样会进行数据对齐,其行索引和列索引分别对齐DataFrame处理缺失数据的相关方法: dropna(axis=0,where=‘any’,…)fillna()isnull()notnull()

df>>>one twoa 1.0 2.0b 2.0 1.0c 3.0 NaNd NaN Nandf.dropna()# 删除NaN>>>one twoa 1.0 2.0b 2.0 1.0df.dropna(how='all')# 删除都是NaN的行>>>one twoa 1.0 2.0b 2.0 1.0c 3.0 NaNdf.dropna(how='any')# 默认,删除所有含有NaN的行>>>one twoa 1.0 2.0b 2.0 1.0df.drop(axis=1)# 默认axis=0按行为标准,写成1后按列为标准

pandas常用函数

df = pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b...: ','c']),'two':pd.Series([5,2,1,6],index=['a','b','c...: ','d'])})>>>one twoa 1.0 5b 2.0 2c 3.0 1d NaN 6df.mean()# 对列(行)求平均值 括号内axis值默认为0,若为1则对行求平均值>>>one 2.0two 3.5dtype: float64df.sum()# 求和 括号内参数与mean()相同>>>one6.0two 14.0dtype: float64df.sort_values(by='具体行',accending=False,axis=1或0) # 其中accending表示升序,False则表示为降序,含有NaN的行不参与排序默认放到最后一行

时间对象

时间对象处理

时间序列类型:

​ 时间戳:特定时刻

​ 固定时间:如7月

​ 时间间隔:起始时间—结束时间

Python标准库处理时间对象:datetime

灵活处理时间对象:datautil

​ dateutil.parser.parse() # 无法处理中文格式 但是可以处理JAN等缩写

成组处理时间对象:pandas

​ pd.to_datetime()

dateutil.parser.parse('20010130')# 可以将任何格式的时间形式转换 >>>datetime.datetime(2001, 1, 30, 0, 0)pd.to_datetime(['2001.1.30','20010813'])# 可以批量处理时间对象 无论格式是否相同>>> DatetimeIndex(['2001-01-30', '2001-08-13'], dtype='datetime64[ns]', freq=None)

时间对象生成

产生时间对象数组:date_range start 开始时间end 结束时间periods 时间长度freq 时间频率,默认为’D’,可选H(our),W(eek),B(usiness),S(emi-)M(onth),(min)T(es),S(econd),A(year),…

pd.date_range('2001.1.30','2001.8.13',periods=10)# periods表示输出的时间个数>>>DatetimeIndex(['2001-01-30 00:00:00', '2001-02-20 16:00:00','2001-03-14 08:00:00', '2001-04-05 00:00:00','2001-04-26 16:00:00', '2001-05-18 08:00:00','2001-06-09 00:00:00', '2001-06-30 16:00:00','2001-07-22 08:00:00', '2001-08-13 00:00:00'],dtype='datetime64[ns]', freq=None)

时间序列

时间序列就是以时间对象为索引的Series或DataFrame。datetime对象作为索引时是存储在DatetimeIndex对象中的。时间序列特殊功能: 传入“年”或“年月”作为切片方式传入日期范围作为切片方式丰富的函数支持:resample(),truncate(),…

sr = pd.Series(np.arange(100),index=pd.date_range('200...: 10130',periods=100))# 首先生成以时间对象为索引的Series>>>2001-01-3002001-01-311..2001-05-09 99Freq: D, Length: 100, dtype: int32sr['2001-03','2001-04']# 然后可以根据具体的日期进行索引,可以对一个时间段进行索引>>>2001-03-01 302001-03-02 31...2001-03-30 592001-03-31 60Freq: D, dtype: int32

#一些函数sr.resample('W').sum()# resample()就是对括号内的单位时间进行重新采样,例如对将每一周的数据和统计出来就是上面这样,括号内的参数与时间对象创建的频率是一样的单位>>>2001-02-04152001-02-11632001-02-18 112...2001-05-13 294Freq: W-SUN, dtype: int32sr.truncate(before='2001-01-30')# 该函数就是输出你输入的时间后的所有记录,功能类似切片,实用性不高,括号内还有after

文件处理

数据文件格式:csv(以某间隔符分割数据)pandas读取文件:从文件名、URL,文件对象中加载数据 read_csv 默认分隔符为逗号read_table 默认分隔符为制表符

文件读取

read_csv,read_table函数主要参数: sep 指定分隔符header=None 指定文件无列名name 指定列名index_col 指定某列作为索引skip_row 指定跳过某些行(不常用)na_values 指定某些字符串表示缺失值parse_dates 指定某些列是否被解析为日期,类型为布尔值或列表

pd.read_csv('training.csv',index_col='Time')# 可以更改索引,这里是将第0列作为索引,index_col的参数也可以是自定义的列名,例如这里的Time>>>ID name scoreTime2001/1/1 01 A982001/1/2 02 B95...2001/1/11 20 K682001/1/12 20 L65# 但是这里的Time序列无法直接进行检索,输出只是index,所以需要将其转化为时间序列pd.read_csv('training.csv',index_col='Time',parse_dates=['Time'])# 可以指定其中一列转化为时间序列,如果parse_dates=True会自动转化# 如果文件没有列名读取时会自动把第一行当作列名,为了避免这种情况会使用下面的方法pd.read_csv('training.csv',header=None)>>>0 1 2 30 2001/1/1 01 A 98...11 2001/1/12 20 L 65pd.read_csv('training.csv',header=None,names=list('abcd'))# 也可以指定输出列名>>>a b c d0 2001/1/1 01 A 981 2001/1/2 02 B 95...11 2001/1/12 20 L 65

文件写入

写入到csv文件:to_csv函数写入文件函数的主要参数: sep 指定文件分隔符na_rep 指定缺失值转换的字符串,默认为空字符串header=False 不输出列名一行index=False 不输出行索引一列columns 指定输出的列,传入列表

df# df[0,0]的位置值为NaN>>>0 1 2 30 NaN 01 A 981 2001/1/2 02 B 95...11 2001/1/12 20 L 65df.to_csv('training.csv', header=False, index=False, na_rep='null', columns=[0,1,2,3])# 此步骤可以将df存储的内容保存至training.csv中

pandas支持其他文件类型:json,XML,HTML,数据库,pickle,excel…

Matplotlib 使用jupyter编译

matplotlib介绍

数据可视化

Matplotlib是一个强大的Pytho绘图和数据可视化的工具包安装方法:pip install matplotlib引用方法:import matplotlib.pyplot as plt绘图函数:plt.plot()显示图像:plt.show()

plt.plot([1,2,3,4],[2,3,1,7])# 折线图,第一个方括号里对应X轴的值,第二个方括号对应Y轴的值plt.plot([1,2,3,4],[2,3,1,7],'o')# 后面有个o是只用点点出来,'o-'的话就是点和线组成

plot函数:绘制折线图 线性linestyle(-,-.,–,…)点型marker(v,^,s,*,H,+,x,D,o,…)颜色color(b,g,r,y,k,w,…) plot函数绘制多条曲线pandas包对plot的支持

plot函数周边

plt.xticks(np.arange(0,11,2),['a','b','c','d','e','f'])# 该行语句其中括号里第一个参数设置了x轴的长度以及步长,第二个参数将x轴的每一个数字替换成英文字母plt.legend()# 这个如果直接打上去是不会有任何的改变的,首先必须每一条线有lable属性,对应图例名称,例如plt.plot([1,2,3,4],[2,3,1,7],label='line 1'),这样才可以在图像空白处显示曲线图例

pandas与Matplotlib

df = pd.read_csv('training.csv', parse_dates=['Date'], index_col='Date')[['Name','Score']]# 这里就是读取了training.csv文件,将Date标签设置为索引,然后再设置了需要显示的变量

题目:Matplotlib实例——绘制数学函数图像

使用Matplotlib模块再一个窗口中绘制数学函数y=x,y=x²,y=3x³+5x²+2x+1的图像,使用不同颜色的线加以区别,并使用图例说明各个线代表什么函数。

x = np.linspace(-100,100,10000)y1 = x.copy()y2 = x ** 2y3 = 3*x**3+5*x**2+2*x+1plt.plot(x,y1,label='y=x',color='red')plt.plot(x,y2,label='y=x^2',color='green')plt.plot(x,y3,label='y=3x^3+5x^2+2x+1',color='blue')plt.ylim(-1000,1000)plt.legend()plt.show()

matplotlib画布与子图

画布:figure fig = plt.figure 图:subplot ax1 = fig.add_subplot(2,2,1) 调节子图间距: subplots_adjust(left,bottom,right,top,wspace,hspace)

# 举例fig = plt.figure()ax1 = fig.add_subplot(2,2,1)ax1.plot([1,2,3,4],[5,4,2,6])ax2 = fig.add_subplot(2,2,2)plt.show()

matplotlib柱状图和饼图

# 柱状图data = [21,22,41,100]# 确定各值labels = ['Jan','Feb','Mar','Apr']# 确定各条的名字plt.bar(np.arange(len(data)),data,color=['red','blue','yellow','green'])# 确定属性,包括条形图的对应位置,值,以及各条的颜色 plt.xticks(np.arange(len(data)),labels)# 改变x轴plt.show()

# 饼图plt.pie([10,20,30,40],labels=['a','b','c','d'],autopct='%.2f%%',explode=[0.1,0,0,0])# 这里面第一个参数是对应饼内各块的值,labels就是各块的标签,autopct可以设置值的显示形式,这里是百分号后显示两位小数,explode是可以让其中几块突出显示,括号内的数值指突出的距离plt.axis('equal')# 刻度等长plt.show

matplotlib K线图

现在已经没有matplotlib.finance子包了,已经单独分离出mplfinance包

mplfinanace包中有许多绘制金融相关图的函数接口。

绘制k线图:mpf.plot()函数

import mplfinance as mpfmpf.plot(data)# data里为传入的数据

修改plot绘图类型

通过参数type修改绘图类型,默认是ohlc,可改为type='candle’或者type=‘line’

mpf.plot(data,type='candle')

增加绘制均线

关键字参数mav=(2,5,10),多条均线使用元组,只绘制一条均线,可以mav=10

mpf.plot(data,type='candle',mav=(2,5,10))mpf.plot(data,type='candle',mav=(10,))

绘制成交量

关键字参数volume=True

mpf.plot(data,type='candle',mav=(2,5,10),volume=True)

自动剔除非交易日空白

关键字参数show_nontrading,默认是False,设置为True,就可以看到停盘的时间段

mpf.plot(data,type='candle',mav=(2,5,10),volume=True,show_nontrading=True)# 由于上述读取的数据没有停盘时间段,因此绘图没有什么不同。

Tushare-金融数据接包口

Tushare是一个免费、开源的python财经数据接口包,更多信息可以上官网查看

需要另外安装一个Tushare的包pip install Tushare

import tushare as tsts.get_k_data('601318')# 可以显示近几年该代码股票的交易情况

股票分析作业

使用tushare包获取某股票的历史行情数据输出该股票所有收盘比开盘上涨3%以上的日期输出该股票所有开盘比前日收盘跌幅超过2%的日期假如我从1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?

import matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport tushare as ts# 获取该股票的历史行情数据df = ts.get_k_data('601318',start='1988-01-01')df.to_csv('601318.csv') # 存储成csv文件# 以时间为索引df = pd.read_csv('601318.csv',index_col='date',parse_dates=['date'])# 输出该股票所有收盘比开盘上涨3%以上的日期df[(df['close']-df['open'])/df['open']>=0.03].index# 输出该股票所有开盘比前日收盘跌幅超过2%的日期df[(df['open']-df['close'].shift(1))/df['close'].shift(1)<=-0.02]#shift()的功能是将该列的数据全部往下移一行,括号内的数字代表向下移几行,不知道该函数也可以用循环做# 假如我从1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?price_last = df['open'][-1]df = df['-01':'-07']df_monthly = df.resample('M').first() # 重新取样,取每个月的第一天的数据并存储df_yearly = df.resample('A').last()[:-1] # 重新取样,取每年的最后一天的数据并存储cost_money = 0hold = 0for year in range(,):cost_money = df_monthly[str(year)]['open'].sum()*100 # 买入所花费的钱hold += len(df_monthly[str(year)]['open'])*100 # 手中持有的股数if year !=:cost_money -= df_yearly[str(year)]['open'][0] * hold # 卖出该年股票后花费的钱hold = 0print (cost_money)cost_money -= hold * price_lastprint (-cost_money)

双均线分析作业

查找历史金叉死叉日期

均线:对于没一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为一条线,就叫做N日移动平均线。移动平均线常用线有5天、10天、30天、60天、120天、240天的指标。 5天和10天的是短线操作的参照指标,称做日均线指标;30天和60天的是中期均线指标,称做季均线指标;120天和240天的是长期均线指标,称做年均线指标。 金叉:短期均线上穿长期均线,买入信号死叉:短期均线下穿长期均线,卖出信号

题目:

使用tushare包获取某股票的历史行情数据使用pandas包计算该股票历史数据的5日均线和60日均线使用matplotlib包可视化历史数据的收盘价和两条均线分析输出所有金叉日期和死叉日期假如我从1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益率如何?

import matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport tushare as ts# 获取该股票的历史行情数据df = ts.get_k_data('600519',start='2001-01-01')df.to_csv('600519.csv',index=False) # 存储成csv文件# 以时间为索引df = pd.read_csv('600519.csv',index_col='date',parse_dates=['date'])[['open','close','low','high']]# 循环方法# df['ma5'] = np.nan# df['ma30'] = np.nan# for i in range(4,len(df)):#df.loc[df.index[i],'ma5'] = df['close'][i-4:i+1].mean()# for i in range(29,len(df)):#df.loc[df.index[i],'ma30'] = df['close'][i-29:i+1].mean()# rolling方法 df['ma5'] = df['close'].rolling(5).mean()df['ma30'] = df['close'].rolling(30).mean()# 画出三条线df[['close','ma5','ma30']].plot()plt.show()# 方法1# golden_cross = []# death_cross = []# for i in range(1,len(df)):#if df['ma5'][i] >= df['ma30'][i] and df['ma5'][i-1] < df['ma30'][i-1]:# golden_cross.append(df.index[i])#if df['ma5'][i] <= df['ma30'][i] and df['ma5'][i-1] > df['ma30'][i-1]:# death_cross.append(df.index[i])# 方法2sr1 = df['ma5'] < df['ma30']sr2 = df['ma5'] >= df['ma30']death_cross = df[sr1 & sr2.shift(1)].indexgolden_cross = df[~(sr1 | sr2.shift(1))].indexfirst_money = 100000money = first_moneyhold = 0 # 持有股数sr1 = pd.Series(1,index=golden_cross)sr2 = pd.Series(0,index=death_cross)sr = sr1.append(sr2).sort_index()for i in range(0,len(sr)):p = df['open'][sr.index[i]]if sr.iloc[i] == 1:# 金叉buy = (money // (100 * p))hold += buy*100money -= buy*100*pelse:# 死叉money += hold * phold = 0p = df['open'][-1] now_money = hold * p + moneyprint(now_money - first_money)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。