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

开发 后端
对最基本的折线图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], <a list of 4 Text xticklabel objects>)

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

 

  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.]), <a list of 10 Text xticklabel objects>)

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

 

  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], <a list of 4 Text xticklabel objects>)

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

 

  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'

[<matplotlib.lines.Line2D at 0x7f6f0352f978>]

 

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

[<matplotlib.lines.Line2D at 0x7f6f0350d438>]

2个参数

自动解析位置参数的原则

(x,y)形式

 

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

[<matplotlib.lines.Line2D at 0x7f6f034735c0>]

 

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

[<matplotlib.lines.Line2D at 0x7f6f03461b38>]

(y,fmt)形式

 

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

 

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

[<matplotlib.lines.Line2D at 0x7f6f033b7cf8>]

 

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

[<matplotlib.lines.Line2D at 0x7f6f033a1cf8>]

3个参数

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

 

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

[<matplotlib.lines.Line2D at 0x7f6f0331e048>]

 

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

[<matplotlib.lines.Line2D at 0x7f6f03289390>]

绘图Line2D

仅画线:绘图的默认情况

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

 

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

[<matplotlib.lines.Line2D at 0x7f6f031f86a0>]

仅画标记

 

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

[<matplotlib.lines.Line2D at 0x7f6f03afcba8>]

画线+标记

 

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

[<matplotlib.lines.Line2D at 0x7f6f031d62e8>]

 

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

[<matplotlib.lines.Line2D at 0x7f6f0317b128>]

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.         ) 

[<matplotlib.lines.Line2D at 0x7f6f02f085c0>]

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

 

  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 = '', # 无法识别 

[<matplotlib.lines.Line2D at 0x7f6f02e6e470>]

data关键字的使用

字典数据

 

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

[<matplotlib.lines.Line2D at 0x7f6f02e52e48>]

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

[<matplotlib.lines.Line2D at 0x7f6f02d7a940>]

总结

定义:

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

 

 

责任编辑:庞桂玉 来源: Python中文社区
相关推荐

2023-06-27 13:46:20

2011-12-21 13:25:33

JavaJFreeChart

2022-02-23 15:17:04

鸿蒙OpenHarmonJacascript

2023-11-10 18:07:42

Python折线图折线

2021-01-08 10:32:24

Charts折线图数据可视化

2022-11-07 08:42:50

iOS 16SwiftUI

2023-11-06 10:04:51

Go语言大数据

2024-01-29 13:02:00

数据可视化库数组

2018-04-24 16:01:46

代码数据可视化Python

2020-06-08 15:06:33

Pandas可视化数据

2023-03-06 08:03:10

Python可视化工具

2022-11-18 09:03:09

SwiftUIiOS16

2020-04-25 20:11:23

Python热力图代码

2009-07-15 16:18:08

JSci.swing包

2021-03-05 07:03:38

Pyecharts可视化工具复合图

2021-04-20 09:56:58

Python 开发编程语言

2022-09-29 15:24:11

物联网MQTT

2015-07-22 10:57:36

watchOS图表自定义

2017-01-10 15:14:34

大数据数据可视化图表类型

2016-12-27 10:19:42

JavaScriptindexOf
点赞
收藏

51CTO技术栈公众号