当前位置: 首页 > 科技观察

matplotlibplot()中折线图方法超详解plot()

时间:2023-03-13 22:51:31 科技观察

importpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltimportmatplotlib目的对最基本的折线图plot进行详解,为绘制其他类型的图形打下坚实的基础。plt.plot()的定义和调用定义:plt.plot(*args,scalex=True,scaley=True,data=None,**kwargs)call:plot([x],y,[fmt],*,data=None,**kwargs)plot([x],y,[fmt],[x2],y2,[fmt2],...,**kwargs)位置参数:[x],y,[fmt]关键字参数传递:*下面参数x序列的不同类型的text-typex序列#dataX=[8,3,5,'t']#会按照[0,1,2,3的顺序定位到x]在轴的刻度上Y=[1,2,3,4]plt.plot(X,Y,marker='o',c='g')ax=plt.gca()print('x-轴刻度:',plt.xticks())#listxticklabels_lst=ax.get_xticklabels()print('-'*70)x轴刻度:([0,1,2,3],)-----------------------------------------------------------------print('x轴刻度标签:',list(xticklabels_lst))#是文本标签x轴刻度标签:[Text(0,0,'8'),Text(1,0,'3'),Text(2,0,'5'),Text(3,0,'t')]数字x序列#dataX=[8,3,5,1]#会根据数字定位到x轴的刻度上[8,3,5,1]Y=[1,2,3,4]plt.plot(X,Y,marker='o',c='g')ax=plt.gca()打印('x轴刻度:',plt.xticks())#arrayxticklabels_lst=ax.get_xticklabels()print('-'*70)x轴刻度:(数组([0.,1.,2.,3.,4.,5.,6.,7.,8.,9.]),<10个文本xticklabel对象的列表>)----------------------------------------------------------------print('x轴刻度标签:',list(xticklabels_lst))#是按序号排列的文本标签x轴刻度标签:[Text(0.0,0,'0'),文本(1.0,0,'1'),文本(2.0,0,'2'),文本(3.0,0,'3'),文本(4.0,0,'4'),文本(5.0,0,'5'),文本(6.0,0,'6'),文本(7.0,0,'7'),文本(8.0,0,'8'),文本(9.0,0,'9')]2种-2行#dataX1=[8,3,5,'t']X2=[8,3,5,1]Y=[1,2,3,4]plt.plot(X2,Y,marker='o',c='r')plt.plot(X1,Y,marker='o',c='g')ax=plt.gca()print('x轴Scale:',plt.xticks())xticklabels_lst=ax.get_xticklabels()print('-'*70)x轴刻度:([0,1,2,3],)-----------------------------------------------------------------print('x轴刻度标签:',list(xticklabels_lst))x轴刻度标签:[Text(0,0,'8'),Text(1,0,'3'),Text(2,0,'5'),Text(3,0,'t')]提供不同数量的位置参数,几种调用方式参数#return空列表plt.plot()[]plot([x],y,[fmt],*,data=None,**kwargs)plot([x],y,[fmt],[x2],y2,[fmt2],...,**kwargs)1个参数#提供一个数字(点)plt.plot(4.5,marker='o')[]#提供数列plt.plot([4.5,2,3],marker='o')[]2参数自动解析位置参数(x,y)#x/y形式的序列plt.plot([2,1,3],[0.5,2,2.5],marker='o')[]#x/y是标量plt.plot(2,['z'],marker='o')[](y,fmt)形式为#plt.plot(2,'z',marker='o')#Unrecognizedcharacterzinformatstring#y是一个标量plt.plot(2,'r',marker='o')[]#y是一个序列plt.plot([2,1,3],'r--*')[]3个参数([x],y,[fmt])形成plt.plot([2,1,3],[0.5,2,2.5],'p--g',#marker='o'markersize=15)[]#fmt没有写,或者'',然后使用默认样式plt.plot([2,1,3],[0.5,2,2.5],'',#marker='o'markersize=15)[]DrawingLine2D只画线:drawingDefault默认样式:blue[line][nomarker]#marker=None表示不设置plt.plot([2,2.5,1])[]只绘制标记plt.plot([2,2.5,1],'o')[]画线+标记plt.plot([2,2.5,1],'o-')[]plt.plot([2,1,3],'bo--')[]fmt的组合顺序是随机的?6张图和结论#6种组合#[颜色][标记][线],3种任意组合就是6种可能#b:blue#o:circlemark#--:dottedlinefmt=['bo--','b--o','ob--','o--b','--bo','--ob']foriinrange(len(fmt)):plt.subplot(2,3,i+1)plt.plot([2,1,3],fmt[i])#结论:[color][marker][line],每个可选,每个属性可以选择写或者不写't写#与fmt支持的组合[line]中的位置顺序无关-lineLineStyles====字符描述===='-'实线样式'--'虚线风格'-。'dash-dotlinestyle':'dottedlinestylefmtsupported[marker]-markerMarkers====字符描述===='.'pointmarker','pixelmarker\\\'o\\\'circlemarker'v'triangle_downmarker'^'triangle_upmarker'<'triangle_leftmarker'>'triangle_rightmarker'1'tri_downmarker'2'tri_upmarker'3'tri_leftmarker'4'tri_rightmarker's\\\'squaremarker'p'五边形标记'*'星形标记'h'hexagon1标记'H'hexagon2标记'+'plus标记'x'x标记'D'菱形标记'd'thin_diamond标记'|'vlinemarker'_'hlinemarker[color]supportedbyfmt-colorColors支持的颜色缩写是单字母代码====字符颜色===='b'blue'g'green'r'red'c'cyan'm'品红'y'黄色'k'黑色'w'白色所有样式:mark,line,color参考大全链接:https://www.kesci.com/home/project/5ea4e5da105d91002d506ac6Style属性Line属性#Include:(除了颜色)#线型,线宽#linestyleorls:{'-','--','-.',':','',}#linewidthorlw:floatls_lst=['-','--','-.',':',]lw_lst=[1,3,5,7]foriinrange(len(ls_lst)):plt.plot([1,2,3,4],[i+1]*4,ls_lst[i],lw=lw_lst[i])标记属性#Include:'''marker:markerstyle#Border(colorandborderthickness)标记大小markersizeorms:floatmarkevery:Noneorintor(int,int)orsliceorList[int]orfloator(float,float)'''#linestyle=None表示不设置,默认值#linestyle=''linestyle='none'表示没有格式,没有行lt.plot([4,2,1,3],linestyle='none',marker='o',markersize=30,#edgemarkeredgecolor='r',markeredgewidth=5,#facemarkerfacecolor='g',#markerfacecolor='none',#markerfacecolor=None,)[]合成:带有空心圆标记的折线图''??'标记点覆盖在直线上,位于上层层次:[top]spines>marker>line>backgroud[bottom]spines:轴的4个边框spine围住线图'''plt.plot([1,5,3,4],marker='o',markersize=20,#edgemarkeredgecolor='r',markeredgewidth=5,#facemarkerfacecolor='w',#白色,与背景同色,遮盖线条营造镂空视觉效果#markerfacecolor='none',#无色,透明,你会看到线条#markerfacecolor=None,#notset,defaultcolor)#markerfacecolor='',#unrecognizable#markerfacecolor='',#unrecognizable[]datakeyWord用法字典数据#dictionarydatad={'name':list('abcd'),'age':[22,20,18,27]}plt.plot('name','age',ddata=d)[]DataFramedata#DataFramedatad={'name':list('abcd'),'age':[22,20,18,27]}df=pd。DataFrame(d)df名字年龄0a221b202c183d27plt.plot('name','age',data=df)[]摘要定义:plt.plot(*args,scalex=True,scaley=True,data=None,**kwargs)调用:plot([x],y,[fmt],*,data=None,**kwargs)plot([x],y,[fmt],[x2],y2,[fmt2],...,**kwargs)x,y,fmt不能使用关键字传递参数。推荐使用:plt.plot(x,y,fmt)多组数据时,再次调用plt.plot(x2,y2,fmt2)绘制第二条线...默认样式:blue[line]+[未标记],即未标记的线可以使用fmt快速控制线的基础属性:color、line、mark[color][marker][line]fmt和keyword属性可以结合使用。两者冲突时,以关键字为准。对于已有的标注数据如df,可以使用plt.plot('columns_name1','columns_name2',data=df)