超詳解matplotlib中的折線圖方法plot()
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib
目的
- 對最基本的折線圖plot做詳細(xì)的解讀,為繪制其他類型的圖形打好基礎(chǔ)。
plt.plot()的定義及調(diào)用
定義:
- plt.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
調(diào)用:
- plot([x], y, [fmt], *, data=None, **kwargs)
- plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
位置參數(shù):
- [x], y, [fmt]
關(guān)鍵字傳參:
- *后面的參數(shù)
x序列的不同類型
文本型的x序列
- # data
- X = [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()) #list
- xticklabels_lst = ax.get_xticklabels()
- print('-'*70)
x軸刻度:([0, 1, 2, 3], <a list of 4 Text xticklabel objects>)
----------------------------------------------------------------------
- print('x軸刻度標(biāo)簽:',list(xticklabels_lst)) #是個文本標(biāo)簽
x軸刻度標(biāo)簽:[Text(0, 0, '8'), Text(1, 0, '3'), Text(2, 0, '5'), Text(3, 0, 't')]
數(shù)字型的x序列
- # data
- X = [8,3,5,1] # 會按數(shù)字【8,3,5,1】被定位在x軸的刻度上
- Y = [1,2,3,4]
- plt.plot(X,Y,marker = 'o',c='g')
- ax = plt.gca()
- print('x軸刻度:',plt.xticks()) # array
- xticklabels_lst = ax.get_xticklabels()
- print('-'*70)
x軸刻度:(array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]), <a list of 10 Text xticklabel objects>)
----------------------------------------------------------------------
- print('x軸刻度標(biāo)簽:',list(xticklabels_lst)) #是個按序號排列的文本標(biāo)簽
x軸刻度標(biāo)簽:[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條線
- # data
- X1 = [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軸刻度:',plt.xticks())
- xticklabels_lst = ax.get_xticklabels()
- print('-'*70)
x軸刻度:([0, 1, 2, 3], <a list of 4 Text xticklabel objects>)
----------------------------------------------------------------------
- print('x軸刻度標(biāo)簽:',list(xticklabels_lst))
x軸刻度標(biāo)簽:[Text(0, 0, '8'), Text(1, 0, '3'), Text(2, 0, '5'), Text(3, 0, 't')]
提供不同數(shù)量的位置參數(shù)
幾種方式的調(diào)用
無參數(shù)
- #返回一個空列表
- plt.plot()
[]
plot([x], y, [fmt], *, data=None, **kwargs) plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
1個參數(shù)
- #提供一個數(shù)(點)
- plt.plot(4.5,marker='o')
[<matplotlib.lines.Line2D at 0x7f6f0352f978>]
- #提供一個數(shù)字序列
- plt.plot([4.5,2,3],marker='o')
[<matplotlib.lines.Line2D at 0x7f6f0350d438>]
2個參數(shù)
自動解析位置參數(shù)的原則
(x,y)形式
- # x/y 為序列
- plt.plot([2,1,3],[0.5,2,2.5],marker='o')
[<matplotlib.lines.Line2D at 0x7f6f034735c0>]
- # x/y 為標(biāo)量
- plt.plot(2,['z'],marker = 'o')
[<matplotlib.lines.Line2D at 0x7f6f03461b38>]
(y,fmt)形式
- # plt.plot(2,'z',marker = 'o') #Unrecognized character z in format string
- # y 為標(biāo)量
- plt.plot(2,'r',marker = 'o')
[<matplotlib.lines.Line2D at 0x7f6f033b7cf8>]
- # y 為序列
- plt.plot([2,1,3],'r--*')
[<matplotlib.lines.Line2D at 0x7f6f033a1cf8>]
3個參數(shù)
([x],y,[fmt])形式
- plt.plot([2,1,3],[0.5,2,2.5],'p--g',
- # marker='o'
- markersize = 15
- )
[<matplotlib.lines.Line2D at 0x7f6f0331e048>]
- # fmt不寫,或者‘’,則使用默認(rèn)樣式
- plt.plot([2,1,3],[0.5,2,2.5],'',
- # marker='o'
- markersize = 15
- )
[<matplotlib.lines.Line2D at 0x7f6f03289390>]
繪圖Line2D
僅畫線:繪圖的默認(rèn)情況
默認(rèn)樣式:藍(lán)色的【線】【無標(biāo)記】
- # marker = None 表示不做設(shè)置
- plt.plot([2,2.5,1])
[<matplotlib.lines.Line2D at 0x7f6f031f86a0>]
僅畫標(biāo)記
- plt.plot([2,2.5,1],'o')
[<matplotlib.lines.Line2D at 0x7f6f03afcba8>]
畫線+標(biāo)記
- plt.plot([2,2.5,1],'o-')
[<matplotlib.lines.Line2D at 0x7f6f031d62e8>]
- plt.plot([2,1,3],'bo--')
[<matplotlib.lines.Line2D at 0x7f6f0317b128>]
fmt的組合順序隨意的?
6圖合一及結(jié)論
- # 6種組合
- # [color][marker][line],3種任意組合為6種可能
- # b :藍(lán)色
- # o: 圓圈標(biāo)記
- # --:虛線
- fmt = ['bo--','b--o','ob--','o--b','--bo','--ob']
- for i in range(len(fmt)):
- plt.subplot(2,3,i+1)
- plt.plot([2,1,3],fmt[i])
- # 結(jié)論:[color][marker][line],每個都是可選的,每個屬性可以選擇寫或者不寫
- # 而且與組合中它們所在的位置順序無關(guān)
fmt支持的【線】-line
Line Styles
==== character description ====
'-' solid line style '--' dashed line style '-.' dash-dot line style ':' dotted line style
fmt支持的【標(biāo)記】-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
所有樣式:標(biāo)記、線、顏色參考大全
鏈接:https://www.kesci.com/home/project/5ea4e5da105d91002d506ac6
樣式屬性
線條的屬性
- # 包含:(顏色除外)
- # 線的樣式、線的寬度
- # linestyle or ls: {'-', '--', '-.', ':', '', }
- # linewidth or lw: float
- ls_lst = ['-', '--', '-.', ':',]
- lw_lst = [1,3,5,7]
- for i in range(len(ls_lst)):
- plt.plot([1,2,3,4],[i+1]*4,ls_lst[i],lw = lw_lst[i])
標(biāo)記的屬性
- # 包含:
- '''
- marker: marker style
- #邊框(顏色及邊框粗細(xì))
- markeredgecolor or mec: color
- markeredgewidth or mew: float
- #面顏色
- markerfacecolor or mfc: color
- markerfacecoloralt or mfcalt: color #備用標(biāo)記顏色
- #標(biāo)記的大小
- markersize or ms: float
- markevery: None or int or (int, int) or slice or List[int] or float or (float, float)
- '''
- # linestyle = None 表示不做設(shè)置,以默認(rèn)值方式
- # linestyle = '' linestyle = 'none' 表示無格式,無線條
- plt.plot([4,2,1,3],linestyle = 'none',
- marker = 'o',
- markersize = 30,
- # edge
- markeredgecolor = 'r',
- markeredgewidth = 5,
- # face
- markerfacecolor = 'g',
- # markerfacecolor = 'none',
- # markerfacecolor = None,
- )
[<matplotlib.lines.Line2D at 0x7f6f02f085c0>]
綜合:帶有空心圓標(biāo)記的線條圖
- '''
- 標(biāo)記點是覆蓋在線條的上面,位于上層
- 圖層層次:[top] spines > marker > line > backgroud [bottom]
- spines:軸的4個邊框
- spines 將線條圖圍在里面
- '''
- plt.plot([1,5,3,4],
- marker = 'o',
- markersize = 20,
- # edge
- markeredgecolor = 'r',
- markeredgewidth = 5,
- # face
- markerfacecolor = 'w', # 白色,與背景色相同,把線條覆蓋著,營造空心的視覺效果
- # markerfacecolor = 'none', # 無色,透明,會看到線條
- # markerfacecolor = None, # 不設(shè)置,默認(rèn)顏色
- )
- # markerfacecolor = ' ', # 無法識別
- # markerfacecolor = '', # 無法識別
[<matplotlib.lines.Line2D at 0x7f6f02e6e470>]
data關(guān)鍵字的使用
字典數(shù)據(jù)
- #字典數(shù)據(jù)
- d = {'name':list('abcd'),'age':[22,20,18,27]}
- plt.plot('name','age',ddata = d)
[<matplotlib.lines.Line2D at 0x7f6f02e52e48>]
DataFrame數(shù)據(jù)
- #DataFrame數(shù)據(jù)
- d = {'name':list('abcd'),'age':[22,20,18,27]}
- df = pd.DataFrame(d)
- df
name | age | |
---|---|---|
0 | a | 22 |
1 | b | 20 |
2 | c | 18 |
3 | d | 27 |
- plt.plot('name','age',data = df)
[<matplotlib.lines.Line2D at 0x7f6f02d7a940>]
總結(jié)
定義:
- plt.plot(*args,scalex = True,scaley = True,data = None ,**kwargs)
調(diào)用:
- plot([x], y, [fmt], *, data=None, **kwargs)
- plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
x,y,fmt均不能使用關(guān)鍵字傳參
推薦使用:
- plt.plot(x,y,fmt)
- 多組數(shù)據(jù)時,再次調(diào)用
- plt.plot(x2,y2,fmt2) 畫第2條線即可...
- 默認(rèn)樣式:藍(lán)色的【線】+【無標(biāo)記】,即無標(biāo)記的線
- 可使用fmt來快捷控制線條的基本屬性:顏色、線、標(biāo)記
- [color][marker][line]
- fmt與關(guān)鍵字屬性可混合使用,當(dāng)兩者有沖突時,以關(guān)鍵字的為準(zhǔn)。
- 對于已有的帶標(biāo)簽的數(shù)據(jù)如df,可使用
- plt.plot('columns_name1','columns_name2',data = df)