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

[[327436]]

在索县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、网站制作 网站设计制作按需网站策划,公司网站建设,企业网站建设,品牌网站设计,营销型网站,成都外贸网站建设,索县网站建设费用合理。

 

 
 
 
 
  1.  import pandas as pd   
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. import matplotlib  

目的

  •  对最基本的折线图plot做详细的解读,为绘制其他类型的图形打好基础。

plt.plot()的定义及调用

定义:

  •  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]

关键字传参:

  •  *后面的参数

x序列的不同类型

文本型的x序列

 

 
 
 
 
  1. # data  
  2. X = [8,3,5,'t'] # 会按顺序【0,1,2,3】被定位在x轴的刻度上  
  3. Y = [1,2,3,4]  
  4. plt.plot(X,Y,marker = 'o',c='g')  
  5. ax = plt.gca()  
  6. print('x轴刻度:',plt.xticks())  #list  
  7. xticklabels_lst = ax.get_xticklabels()  
  8. print('-'*70) 

x轴刻度:([0, 1, 2, 3], )

----------------------------------------------------------------------

 

 
 
 
 
  1. print('x轴刻度标签:',list(xticklabels_lst))  #是个文本标签 

x轴刻度标签:[Text(0, 0, '8'), Text(1, 0, '3'), Text(2, 0, '5'), Text(3, 0, 't')]

数字型的x序列

 

 
 
 
 
  1. # data  
  2. X = [8,3,5,1] # 会按数字【8,3,5,1】被定位在x轴的刻度上  
  3. Y = [1,2,3,4]  
  4. plt.plot(X,Y,marker = 'o',c='g')  
  5. ax = plt.gca()  
  6. print('x轴刻度:',plt.xticks()) # array  
  7. xticklabels_lst = ax.get_xticklabels()  
  8. print('-'*70) 

x轴刻度:(array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]), )

----------------------------------------------------------------------

 

 
 
 
 
  1. print('x轴刻度标签:',list(xticklabels_lst))  #是个按序号排列的文本标签 

x轴刻度标签:[Text(0.0, 0, '0'), Text(1.0, 0, '1'), Text(2.0, 0, '2'), Text(3.0, 0, '3'), Text(4.0, 0, '4'), Text(5.0, 0, '5'), Text(6.0, 0, '6'), Text(7.0, 0, '7'), Text(8.0, 0, '8'), Text(9.0, 0, '9')]

2种类型-2条线

 

 
 
 
 
  1. # data  
  2. X1 = [8,3,5,'t']  
  3. X2 = [8,3,5,1]  
  4. Y = [1,2,3,4]  
  5. plt.plot(X2,Y,marker = 'o',c='r')  
  6. plt.plot(X1,Y,marker = 'o',c='g')  
  7. ax = plt.gca()  
  8. print('x轴刻度:',plt.xticks())  
  9. xticklabels_lst = ax.get_xticklabels()  
  10. print('-'*70) 

x轴刻度:([0, 1, 2, 3], )

----------------------------------------------------------------------

 

 
 
 
 
  1. print('x轴刻度标签:',list(xticklabels_lst))  

x轴刻度标签:[Text(0, 0, '8'), Text(1, 0, '3'), Text(2, 0, '5'), Text(3, 0, 't')]

提供不同数量的位置参数

几种方式的调用

无参数

 

 
 
 
 
  1. #返回一个空列表  
  2. plt.plot() 

[]

plot([x], y, [fmt], *, data=None, **kwargs) plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

1个参数

 

 
 
 
 
  1. #提供一个数(点)  
  2. plt.plot(4.5,marker='o') 

[]

 

 
 
 
 
  1. #提供一个数字序列  
  2. plt.plot([4.5,2,3],marker='o') 

[]

2个参数

自动解析位置参数的原则

(x,y)形式

 

 
 
 
 
  1. # x/y 为序列  
  2. plt.plot([2,1,3],[0.5,2,2.5],marker='o') 

[]

 

 
 
 
 
  1. # x/y 为标量  
  2. plt.plot(2,['z'],marker = 'o')  

[]

(y,fmt)形式

 

 
 
 
 
  1. # plt.plot(2,'z',marker = 'o') #Unrecognized character z in format string 

 

 
 
 
 
  1. # y 为标量   
  2. plt.plot(2,'r',marker = 'o')  

[]

 

 
 
 
 
  1. # y 为序列  
  2. plt.plot([2,1,3],'r--*')  

[]

3个参数

([x],y,[fmt])形式

 

 
 
 
 
  1. plt.plot([2,1,3],[0.5,2,2.5],'p--g',  
  2. #          marker='o'  
  3.          markersize = 15  
  4.         ) 

[]

 

 
 
 
 
  1. # fmt不写,或者‘’,则使用默认样式  
  2. plt.plot([2,1,3],[0.5,2,2.5],'',  
  3. #          marker='o'  
  4.          markersize = 15  
  5.         ) 

[]

绘图Line2D

仅画线:绘图的默认情况

默认样式:蓝色的【线】【无标记】

 

 
 
 
 
  1. # marker = None 表示不做设置  
  2. plt.plot([2,2.5,1]) 

[]

仅画标记

 

 
 
 
 
  1. plt.plot([2,2.5,1],'o') 

[]

画线+标记

 

 
 
 
 
  1. plt.plot([2,2.5,1],'o-') 

[]

 

 
 
 
 
  1. plt.plot([2,1,3],'bo--') 

[]

fmt的组合顺序随意的?

6图合一及结论

 

 
 
 
 
  1. # 6种组合  
  2. # [color][marker][line],3种任意组合为6种可能  
  3. # b :蓝色  
  4. # o: 圆圈标记  
  5. # --:虚线  
  6. fmt = ['bo--','b--o','ob--','o--b','--bo','--ob']  
  7. for i in range(len(fmt)):  
  8.     plt.subplot(2,3,i+1)  
  9.     plt.plot([2,1,3],fmt[i])   
  10. # 结论:[color][marker][line],每个都是可选的,每个属性可以选择写或者不写  
  11. # 而且与组合中它们所在的位置顺序无关 

fmt支持的【线】-line

Line Styles

==== character description ====

 '-' solid line style '--' dashed line style '-.' dash-dot line style ':' dotted line style 

fmt支持的【标记】-marker

Markers

==== character description ====

 '.' point marker ',' pixel marker \\\'o\\\' circle marker 'v' triangle_down marker '^' triangle_up marker '<' triangle_left marker '>' triangle_right marker '1' tri_down marker '2' tri_up marker '3' tri_left marker '4' tri_right marker 's\\\' square marker 'p' pentagon marker '*' star marker 'h' hexagon1 marker 'H' hexagon2 marker '+' plus marker 'x' x marker 'D' diamond marker 'd' thin_diamond marker '|' vline marker '_' hline marker

fmt支持的【颜色】-color

Colors

The supported color abbreviations are the single letter codes

==== character color ====

 'b' blue 'g' green 'r' red 'c' cyan 'm' magenta 'y' yellow 'k' black 'w' white 

所有样式:标记、线、颜色参考大全

链接:https://www.kesci.com/home/project/5ea4e5da105d91002d506ac6

样式属性

线条的属性

 

 
 
 
 
  1. # 包含:(颜色除外)  
  2. # 线的样式、线的宽度  
  3. # linestyle or ls: {'-', '--', '-.', ':', '', }  
  4. # linewidth or lw: float  
  5. ls_lst = ['-', '--', '-.', ':',]   
  6. lw_lst = [1,3,5,7]  
  7. for i in range(len(ls_lst)):  
  8.     plt.plot([1,2,3,4],[i+1]*4,ls_lst[i],lw = lw_lst[i]) 

标记的属性

 

 
 
 
 
  1. # 包含:  
  2. '''  
  3. marker: marker style  
  4. #边框(颜色及边框粗细)  
  5. markeredgecolor or mec: color  
  6. markeredgewidth or mew: float  
  7. #面颜色  
  8. markerfacecolor or mfc: color  
  9. markerfacecoloralt or mfcalt: color  #备用标记颜色  
  10. #标记的大小  
  11. markersize or ms: float  
  12. markevery: None or int or (int, int) or slice or List[int] or float or (float, float)  
  13. '''  
  14. # linestyle = None 表示不做设置,以默认值方式  
  15. # linestyle = ''  linestyle = 'none' 表示无格式,无线条  
  16. plt.plot([4,2,1,3],linestyle = 'none',   
  17.          marker = 'o',  
  18.          markersize = 30,  
  19.          # edge  
  20.          markeredgecolor = 'r',  
  21.          markeredgewidth = 5,  
  22.          # face   
  23.          markerfacecolor = 'g',  
  24. #          markerfacecolor = 'none',  
  25. #          markerfacecolor = None,  
  26.         ) 

[]

综合:带有空心圆标记的线条图

 

 
 
 
 
  1. '''  
  2. 标记点是覆盖在线条的上面,位于上层  
  3. 图层层次:[top]  spines > marker > line > backgroud  [bottom]  
  4. spines:轴的4个边框  
  5. spines 将线条图围在里面  
  6. '''  
  7. plt.plot([1,5,3,4],   
  8.          marker = 'o',  
  9.          markersize = 20,  
  10.          # edge  
  11.          markeredgecolor = 'r',  
  12.          markeredgewidth = 5,  
  13.          # face   
  14.          markerfacecolor = 'w',    # 白色,与背景色相同,把线条覆盖着,营造空心的视觉效果  
  15. #          markerfacecolor = 'none', # 无色,透明,会看到线条  
  16. #          markerfacecolor = None, # 不设置,默认颜色  
  17.         )  
  18. # markerfacecolor = ' ', # 无法识别  
  19. # markerfacecolor = '', # 无法识别 

[]

data关键字的使用

字典数据

 

 
 
 
 
  1. #字典数据  
  2. d = {'name':list('abcd'),'age':[22,20,18,27]}  
  3. plt.plot('name','age',ddata = d) 

[]

DataFrame数据

 

 
 
 
 
  1. #DataFrame数据  
  2. d = {'name':list('abcd'),'age':[22,20,18,27]}  
  3. df = pd.DataFrame(d) 
  4. df 
  name age
0 a 22
1 b 20
2 c 18
3 d 27

 

 
 
 
 
  1. plt.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) 画第2条线即可...
  •  默认样式:蓝色的【线】+【无标记】,即无标记的线
  •  可使用fmt来快捷控制线条的基本属性:颜色、线、标记
  •  [color][marker][line]
  •  fmt与关键字属性可混合使用,当两者有冲突时,以关键字的为准。
  •  对于已有的带标签的数据如df,可使用
  •  plt.plot('columns_name1','columns_name2',data = df)

 

 

文章标题:超详解matplotlib中的折线图方法plot()
网页地址:http://www.shufengxianlan.com/qtweb/news0/555350.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联