中职专业课 计算机类《 Python程序设计任务驱动式教程》人邮版(2023)第十一章Python科学计算与数据分析开发基础 课件

资源下载
  1. 二一教育资源

中职专业课 计算机类《 Python程序设计任务驱动式教程》人邮版(2023)第十一章Python科学计算与数据分析开发基础 课件

资源简介

(共94张PPT)
Python科学计算与数据分析开发基础
软件生态系统scipy
11.1
Python
SciPy特征基于Python的软件生态系统(ecosystem)开源主要为数学、科学和工程服务PythonNumPy特征高性能科学计算和数据分析的基础包强大的ndarray对象精巧的函数和ufunc函数适合线性代数和随机数处理等科学计算>>>importnumpyasnp>>>xArray=np.ones((3, 4))SourcePythonSciPylibrary特征Python中科学计算程序的核心包有效计算numpy矩阵,让NumPy和SciPylibrary协同工作致力于科学计算中常见问题的各个工具箱,其不同子模块有不同的应用,如插值、积分、优化和图像处理等>>>importnumpyasnp>>>fromscipyimportlinalg>>>arr=np.array([[1, 2], [3, 4]])>>>linalg.det(arr)-2.0SourcePythonMatplotlib特征基于NumPy二维绘图库,简单快速地生成曲线图、直方图和散点图等形式的图常用的pyplot是一个简单提供类似MATLAB接口的模块Pythonpandas特征基于SciPylibrary和NumPy高效的Series和DataFrame数据结构强大的可扩展数据操作与分析的Python库高效处理大数据集的切片等功能提供优化库功能读写多种文件格式,如CSV、HDF5…>>>df[2 : 5]>>>df.head(4)>>>df.tail(3)SourcePythonPython常用的数据结构数值型字符串列表元组字典集合Python其他数据结构?SciPy中的数据结构Python原有数据结构的变化ndarray(N维数组)Series(变长字典)DataFrame(数据框)Pythonnumpy
11.2
Python
Python中的数组用list和tuple等数据结构表示数组一维数组list = [1,2,3,4]二维数组list = [[1,2,3],[4,5,6],[7,8,9]]array模块通过array函数创建数组,array.array("B", range(5))提供append、insert和read等方法Pythonndarrayndarray是什么 N维数组NumPy中基本的数据结构所有元素是同一种类型别名为array利于节省内存和提升计算性能有丰富的函数00000000000000000000000000123456789101112131415161718192021222324Python11.2.1ndarray的基本特性Pythonndarray基本概念ndarray数组属性N维数组维度(dimensions)称为轴(axes),轴的个数称为秩(rank)沿着第0轴和第1轴操作axis = 0(按列)axis = 1(按行)000000000000123456789101112第0轴第1轴Pythonndarray基本概念ndarray数组属性N维数组基本属性ndarray.ndim(秩)ndarray.shape(维度)ndarray.size(元素总个数)ndarray.dtype(元素类型)ndarray.itemsize(元素字节大小)000000000000123456789101112第0轴第1轴Python11.2.2创建ndarrayPythonndarray的创建>>>importnumpyasnp>>>aArray=np.array([1,2,3])>>>aArrayarray([1, 2, 3])>>> bArray =np.array([(1,2,3),(4,5,6)])>>> bArrayarray([[1, 2, 3],[4, 5, 6]])>>>bArray.ndim,bArray.shape,bArray.dtype(2, (2, 3),dtype('int32'))Sourcearray()函数Pythonndarray的创建ndarray创建函数arangearraycopyemptyempty_likeeyefromfilefromfunctionidentitylinspacelogspacemgridogridonesones_likerzeroszeros_likePythonndarray的创建>>>np.ones([2, 3])array([[ 1., 1., 1.],[ 1., 1., 1.]])>>>np.zeros((2, 2))array([[ 0., 0.],[0., 0.]])>>> np.arange(1, 5, 0.5)array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])>>>np.linspace(1, 2, 10,endpoint =False)array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])Sourceones()zeros()arange()linspace()Pythonndarray的创建>>>np.random.random((2, 2))array([[ 0.79777004, 0.1468679 ],[ 0.95838379, 0.86106278]])>>> np.fromfunction(lambdai, j:(i+1)*(j+1), (9,9))array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9.],[ 2., 4., 6., 8., 10., 12., 14., 16., 18.],[ 3., 6., 9., 12., 15., 18., 21., 24., 27.],[ 4., 8., 12., 16., 20., 24., 28., 32., 36.],[ 5., 10., 15., 20., 25., 30., 35., 40., 45.],[ 6., 12., 18., 24., 30., 36., 42., 48., 54.],[ 7., 14., 21., 28., 35., 42., 49., 56., 63.],[ 8., 16., 24., 32., 40., 48., 56., 64., 72.],[ 9., 18., 27., 36., 45., 54., 63., 72., 81.]])Sourcerandom()fromfunction()Python11.2.3ndarray的操作和运算Pythonndarray的基本操作-切片>>>aArray=np.array([(1, 2, 3), (4, 5, 6)])array([[1, 2, 3],[4, 5, 6]])>>>print(aArray[1])[4 5 6]>>>print(aArray[0: 2])[[1 2 3][4 5 6]]>>>print(aArray[:, [0, 1]])[[1 2][4 5]]>>>print(aArray[1, [0, 1]])[4 5]Source000000123456Pythonndarray的基本操作-改变数组形状>>>aArray=np.array([(1,2,3),(4,5,6)])>>>aArray.shape(2, 3)>>> bArray =aArray.reshape(3,2)>>> bArrayarray([[1, 2],[3, 4],[5, 6]])>>>aArrayarray([[1, 2, 3],[4, 5, 6]])Source>>>aArray.resize(3,2)>>>aArrayarray([[1, 2],[3, 4],[5, 6]])SourcePythonndarray的基本操作-堆叠>>> bArray=np.array([1,3,7])>>>cArray=np.array([3,5,8])>>> np.vstack((bArray, cArray))array([[1, 3, 7],[3, 5, 8]])>>>np.hstack((bArray, cArray))array([1, 3, 7, 3, 5, 8])SourcePythonndarray的运算>>>aArray=np.array([(5,5,5),(5,5,5)])>>> bArray =np.array([(2,2,2),(2,2,2)])>>> cArray =aArray* bArray>>> cArrayarray([[10, 10, 10],[10, 10, 10]])>>>aArray+= bArray>>>aArrayarray([[7, 7, 7],[7, 7, 7]])Source/*-+>利用基本运算符Pythonndarray的运算>>>a =np.array([1,2,3])>>> b =np.array([[1,2,3],[4,5,6]])>>>a + barray([[2, 4, 6],[5, 7, 9]])Source广播功能较小的数组会广播到较大数组的大小,使它们的形状兼容000000123456000000123123扩展000000246579SciPy生态系统中有许多暗藏的广播功能,例如np.array([1,2,3]) * 3Pythonndarray的运算—统计>>>aArray=np.array([(6,5,4),(3,2,1)])>>>aArray.sum()21>>>aArray.sum(axis = 0)array([9,7,5])>>>aArray.sum(axis = 1)array([15, 6])>>>aArray.min()# return value1>>>aArray.argmin()# return index5Source利用基本数组统计方法summeanstdvarminmaxargminargmaxcumsumcumprodPythonndarray的运算—统计>>>aArray=np.array([(6,5,4),(3,2,1)])>>>aArray.mean()3.5>>>aArray.var()2.9166666666666665>>>aArray.std()1.707825127659933Source利用基本数组统计方法summeanstdvarminmaxargminargmaxcumsumcumprodPython11.2.4ufunc函数Pythonndarray的ufunc函数add, all, any,arange,apply_along_axis,argmax,argmin,argsort, average,bincount, ceil, clip,conj,corrcoef,cov, cross,cumprod,cumsum, diff, dot,exp, floor, …ufunc(universalfunction,通用)是一种能对数组的每个元素进行操作的函数。NumPy内置的许多ufunc函数都是在C语言级别实现的,计算速度非常快,数据量大时有很大的优势。Pythonndarray的ufunc函数#Filename:prog11-1.pyimporttimeimportmathimportnumpyasnpx=np.arange(0, 100, 0.01)t_m1=time.clock()fori, tinenumerate(x):x[i] =math.pow((math.sin(t)), 2)t_m2=time.clock()y =np.arange(0,100,0.01)t_n1=time.clock()y =np.power(np.sin(y), 2)t_n2=time.clock()FileRunningtime of math:t_m2 -t_m1Runningtime ofnumpy:t_n2 -t_n1Python11.2.5专门的应用Pythonndarray的专门应用—线性代数Source>>>importnumpyasnp>>> x=np.array([[1,2], [3,4]])>>> r1=np.linalg.det(x)>>>print(r1)-2.0>>> r2=np.linalg.inv(x)>>>print(r2)[[-2. 1. ][ 1.5 -0.5]]>>> r3= np.dot(x, x)>>>print(r3)[[ 7 10][15 22]]dot矩阵内积linalg.det行列式linalg.inv逆矩阵linalg.solve多元一次方程组求根linalg.eig求特征值和特征向量常用函数示例Pythonpandas
11.3
Python
11.3.1 SeriesPythonSeries基本特征类似一维数组的对象由数据和索引组成(有序字典,也称变长字典)importpandasaspd>>>aSer=pd.Series([1, 2.0,'a'])>>> aSer0 11 22 adtype:objectSourceSeries()函数Python自定义Series的index>>>bSer=pd.Series(['apple','peach','lemon'], index = [1,2,3])>>>bSer1 apple2 peach3 lemondtype: object>>>bSer.index#常进行单独赋值Int64Index([1, 2, 3],dtype= 'int64')>>>bSer.valuesarray(['apple', 'peach', 'lemon'],dtype= object)SourcePythonSeries的基本运算>>>cSer=pd.Series([3, 5, 7], index= ['a','b','c'])>>>cSer['b']5>>>cSer* 2a6b 10c 14dtype: int64>>>importnumpyasnp>>>np.exp(cSer)a20.085537b148.413159c 1096.633158dtype: float64SourcePythonSeries的基本运算>>>cSer=pd.Series([3, 5, 7], index = ['a','b','c'])>>>cSer[1: 2]b 5dtype: int64>>>cSer['a':'b']a 3b 5dtype: int64Source切片基于位置基于索引PythonSeries的数据对齐>>>data ={'AXP':'86.40','CSCO':'122.64','BA':'99.44'}>>> sindex = ['AXP','CSCO','BA','AAPL']>>> aSer =pd.Series(data, index = sindex)>>> aSerAXP 86.40CSCO122.64BA 99.44AAPLNaNdtype:objectSource>>>pd.isnull(aSer)AXP FalseCSCO FalseBA FalseAAPL Truedtype: boolSourcePythonSeries的数据对齐>>> aSer =pd.Series(data, index = sindex)>>> aSerAXP 86.40CSCO122.64BA 99.44AAPLNaNdtype: object>>>bSer= {'AXP':'86.40','CSCO':'122.64','CVX':'23.78'}>>>cSer=pd.Series(bSer)>>> aSer +cSerAAPLNaNAXP 86.4086.40BANaNCSCO 122.64122.64CVXNaNdtype: objectSource重要功能在算术运算中自动对齐不同索引的数据Python11.3.2 DataFramePythonDataFrame基本特征一个表格型的数据结构(称数据框)含有一组有序的列(类似于index)大致可看成共享同一个index的Series集合name pay0Mayue30001Lilin45002Wuyun8000Python创建DataFrame>>>data = {'name': ['Mayue','Lilin','Wuyun'],'pay': [3000, 4500, 8000]}>>> aDF =pd.DataFrame(data)>>> aDFname pay0Mayue30001Lilin45002Wuyun8000SourceDataFrame()函数PythonDataFrame的索引和值>>>data =np.array([('Mayue', 3000), ('Lilin', 4500), ('Wuyun', 8000)])>>>bDF=pd.DataFrame(data, index =range(1, 4),columns = ['name','pay'])>>>bDFname pay1Mayue30002Lilin45003Wuyun8000>>>bDF.index#重新赋值即为修改行索引RangeIndex(start=1, stop=4, step=1)>>>bDF.columns#重新赋值即为修改列索引Index(['name', 'pay'],dtype='object')>>>bDF.valuesarray([['Mayue', '3000'],['Lilin', '4500'],['Wuyun', '8000']],dtype=object)SourcePython修改DataFrame-添加列>>>aDF['tax'] = [0.05, 0.05, 0.1]>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun80000.1Source>>> aDFname pay0Mayue30001Lilin45002Wuyun8000Python修改DataFrame-添加行>>>aDF.loc[5] = {'name':'Liuxi','pay': 5000,'tax': 0.05}>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun80000.15Liuxi5000 0.05Source>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.1Python修改DataFrame-添加行>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.15Liuxi5000 0.05>>>tempDFname pay tax7Yeqing7000 0.19Qianjie9500 0.1>>>aDF.append(tempDF)name pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.15Liuxi5000 0.057Yeqing7000 0.19Qianjie9500 0.1SourcePython修改DataFrame-添加行>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.15Liuxi5000 0.05>>>tempDFname pay tax7Yeqing7000 0.19Qianjie9500 0.1>>> pieces = [aDF,tempDF]>>>pd.concat(pieces)name pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun80000.15Liuxi5000 0.057Yeqing7000 0.19Qianjie9500 0.1SourcePython删除>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.15Liuxi5000 0.05>>>aDF.drop(5)name pay tax0Mayue30000.051Lilin4500 0.052Wuyun8000 0.1>>>aDF.drop('tax', axis = 1)name pay0Mayue30001Lilin45002Wuyun80005Liuxi5000SourcedelaDF['tax']直接删除Python修改DataFrame>>>aDF['tax'] =0.03>>> aDFname pay tax0Mayue3000 0.031Lilin4500 0.032Wuyun8000 0.03Liuxi50000.03>>> aDF.loc[5] = ['Liuxi', 9800, 0.05]name pay tax0Mayue3000 0.031Lilin4500 0.032Wuyun8000 0.035Liuxi9800 0.05Source>>> aDFname pay tax0Mayue3000 0.051Lilin4500 0.052Wuyun8000 0.15Liuxi5000 0.05PythonDataFrame数据存取score.csvPythonDataFrame数据存取-读csv文件# Filename: read_csv.pyimportpandasaspd>>> data =pd.read_csv('score.csv', encoding ='gb2312')>>> data姓名语文 数学英语总分0陈纯88 87 85 2601方小磊93 88 90 2712王妤82 99 96 2773彭子晖97 94 84 2754丁海斌97 94 76 267FilePythonDataFrame数据存取-写csv文件# Filename:to_csv.pyimportpandasaspddf =pd.DataFrame(data)df.to_csv('score_copy.csv')FilePythonDataFrame数据存取-读写excel文件#Filename:excel_rw.pyimportpandasaspddf=pd.read_excel('score.xlsx')df.to_excel('score.xlsx',sheet_name='score')FilePythonDataFrame数据选择选择方式选择行选择列选择区域筛选(条件选择)PythonDataFrame数据选择-选择行>>>df['a':'c']>>>df[0: 3]>>>df.head(3)Source选择行索引切片专门的方法>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267PythonDataFrame数据选择-选择列选择列列名不支持df['姓名','语文']df['语文': '英语']>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df['姓名']>>> df.姓名SourcePythonDataFrame数据选择-选择区域选择区域标签(loc)位置(iloc)>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df.loc['b':'d','语文':'英语']>>>df.iloc[1: 4, 1: 4]SourcePythonDataFrame数据选择-选择区域选择区域-行或列标签(loc)位置(iloc)>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df.loc['a':'c',]>>>df.loc[:, ['语文','数学']]>>>df.iloc[:, [1, 2, 3]]Source单独列索引用[]PythonDataFrame数据选择-选择区域选择区域-单个值标签(loc或at)位置(iloc或iat)>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df.at['b','数学']>>>df.iat[1, 2]SourcePythonix-选择行>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df.ix['a']#或df.ix[0]姓名 陈纯语文88数学87英语85总分260Name: a,dtype: objectSourceixloc和iloc的混合Pythonix-选择列>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>>df.ix[:, ['总分']]总分a 260b 271c 277d 275e 267Sourceixloc和iloc的混合df.ix[:,[4]]Pythonix-选择区域>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>> df.ix[:,'语文':'英语']语文数学 英语a 88 87 85b 93 88 90c 82 99 96d 97 94 84e 97 94 76Sourceixloc和iloc的混合df.ix[:,1:4]PythonDataFrame数据选择-条件筛选找出索引值在'b'~'d'之间(包括'b'和'd')并且数学成绩大于等于90的学生记录>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267>>> df[(df.index>='b') & (df.index<='d') & (df.数学>= 90)]SourcePython11.3.3 Series和DataFrame数据统计与分析Python数据统计与分析importpandasaspd>>>dir(pd.Series)[…, 'head',…, 'index', …, 'stack','std',…, 'where',…]>>>dir(pd.DataFrame)[…, 'head', …,'index',…, 'stack','std',…, 'to_csv', …]SourcePython数据统计与分析-简单统计>>>df.mean()语文91.4数学92.4英语86.2总分270.0dtype: float64>>>df.数学.mean()92.4Source>>>df姓名语文数学英语总分a陈纯88 87 85 260b方小磊93 88 90 271c王妤82 99 96 277d彭子晖97 94 84 275e丁海斌97 94 76 267Python数据统计与分析-排序>>>df.sort_values(by ='总分')姓名语文数学英语总分a陈纯88 87 85 260e丁海斌97 94 76 267b方小磊9388 90 271d彭子晖9794 84 275c王妤8299 96 277>>>df.sort_values(by ='总分')[:3].姓名a陈纯e丁海斌b方小磊Name:姓名,dtype:objectSourcePython数据统计与分析-简单统计与筛选>>>df[(df.数学>= 90)].mean()语文92.000000数学95.666667英语85.333333总分273.000000dtype: float64>>>len(df[(df.总分>= 270)])3Source统计数学成绩大于等于90的学生每门课程(包括总分)的平均值统计总分大于等于270的学生人数Python数据统计与分析-简单统计与筛选>>> mark = ['A'ifitem >= 270else'B'foritemindf.总分]>>> df['等级'] = mark>>> df姓名语文 数学 英语 总分 等级a陈纯88 87 85 260 Bb方小磊93 88 90 271 A…>>>df.groupby('等级').姓名.count()等级A 3B 2Name:姓名,dtype:int64Source按总分是否大于等于270为界将等级分为A和B两级Pythonmatplotlib
11.4
Python
Matplotlib绘图Matplotlib绘图最著名Python绘图库,主要用于二维绘图画图质量高方便快捷的绘图模块绘图API——pyplot模块集成库——pylab模块(包含NumPy和pyplot中的常用函数)Python11.4.1Matplotlib绘图基本方法Python折线图>>>importmatplotlib.pyplotasplt>>>plt.plot([3, 4, 7, 6, 2, 8, 9])Sourceplt.plot(range(7), [3, 4, 7, 6, 2, 8, 9])Python折线图-绘制多组数据NumPy数组也可以作为Matplotlib的参数多组成对数据绘图>>>importnumpyasnp>>>importmatplotlib.pyplotasplt>>>t=np.arange(0.,4.,0.1)>>>plt.plot(t, t, t, t+2, t, t**2)SourcePythonpylab绘图>>>importnumpyasnp>>>importpylabaspl>>>t=np.arange(0.,4.,0.1)>>>pl.plot(t, t, t, t+2, t, t**2)SourcePython绘制其他类型的图>>>importnumpyasnp>>>importmatplotlib.pyplotasplt>>>plt.scatter(range(7), [3, 4, 7, 6, 2, 8, 9])>>>plt.bar(range(7), [3, 4, 7, 6, 2, 8, 9])SourcePython11.4.2Matplotlib图形属性控制PythonMatplotlib属性文字和字体属性坐标轴和网格属性子图(axes)子区(subplots)色彩和样式线宽每英寸点数图像大小Matplotlib可以控制的默认属性……Python色彩和样式plt.plot(range(7), [3, 4, 7, 6, 2, 8, 9],'g--')plt.plot(range(7), [3, 4, 7, 6, 2, 8, 9],'rD')Python色彩和样式符号颜色bblueggreenrredccyanmmagentaYyellowkblackwwhite线型描述'-'solid'--'dashed'-.'dash_dot':'dotted'None'draw nothing' 'draw nothing''draw nothing标记描述"o"circle"v"triangle_down"s"square"p"pentagon"*"star"h"hexagon1"+"plus"D"diamond……Python多种属性# Filename:prog11-2.pyimportpylabasplimportnumpyasnppl.figure(figsize=(8,6),dpi=100)t=np.arange(0.,4.,0.1)pl.plot(t,t,color='red',linestyle='-',linewidth=3,label='Line 1')pl.plot(t,t+2,color='green',linestyle='',marker='*',linewidth=3,label='Line 2')pl.plot(t,t**2,color='blue',linestyle='',marker='+',linewidth=3,label='Line 3')pl.legend(loc='upper left')FilePython文字加标题:图、横轴和纵轴#Filename:prog11-3.pyimportmatplotlib.pyplotaspltplt.title('Plot Example')plt.xlabel('X label')plt.ylabel('Y label')plt.plot(range(7), [3, 4, 7, 6, 2, 8, 9])FilePython绘制子图在Matplotlib中绘图在当前图形(figure)和当前坐标系(axes)中进行,默认在一个编号为1的figure中绘图,可以在一个图的多个区域分别绘图使用subplot()函数和axes()函数Python多子图-subplotsplt.subplot(211)plt.subplot(212)plt.subplot(121)plt.subplot(122)plt.subplot(221)plt.subplot(222)plt.subplot(223)plt.subplot(224)Python多子图-subplots#Filename: Prog11-3.pyimportmatplotlib.pyplotaspltplt.figure(1)#默认创建,缺省plt.subplot(211)#第一个子图plt.plot(range(7), [3, 4, 7, 6, 2, 8, 9], color ='r', marker ='o')plt.subplot(212)#第二个子图plt.plot(range(7), [5, 1, 8, 2, 6, 9, 4], color ='green', marker ='o')FilePython多子图-subplots#Filename:Prog11-4.pyimportmatplotlib.pyplotaspltfig, (ax0, ax1) =plt.subplots(2, 1)ax0.plot(range(7), [3, 4, 7, 6, 2, 8, 9], color ='r', marker ='o')ax0.set_title('subplot1')plt.subplots_adjust(hspace= 0.5)ax1.plot(range(7), [5, 1, 8, 2, 6, 9, 4], color ='green', marker ='o')ax1.set_title('subplot2')FilePython子图-axes#Filename:Prog11-5.pyimportmatplotlib.pyplotaspltaxes([.1, .1, 0.8, 0.8])plt.plot(range(7), [3, 4, 7, 6, 2, 8, 9], color ='r', marker ='o')plt.axes([.3, .15, 0.4, 0.3])plt.plot(range(7), [5, 1, 8, 2, 6, 9, 4], color ='green', marker ='o')Fileaxes([left,bottom,width,height])参数范围为(0,1)Pythonpandas绘图>>>importpandasaspd>>> data= [3, 4, 7, 6, 2, 8, 9]>>>pDF=pd.DataFrame(data)>>>pDF.plot()SourcePythonpandas绘图#Filename:Prog11-6.pyimportpandasaspdimportmatplotlib.pyplotaspltimportmatplotlibasmplmpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['font.serif'] = ['SimHei']df=pd.read_csv('score.csv', encoding ='gb2312')df.plot(kind='bar')FilePythonpandas绘图#Filename:Prog11-6.py…df =pd.DataFrame(data)df_copy=pd.DataFrame()df_copy['语文'] = df.语文df_copy['数学'] = df.数学df_copy['英语'] = df.英语df_copy.index= df.姓名df_copy.plot(kind='bar')plt.title('学生成绩')plt.xlabel('课程')plt.ylabel('成绩')FilePython小结11.5Python小结SciPy简介NumPy包pandas包Matplotlib包Python

展开更多......

收起↑

资源预览