如何使用matplotlib中的折線圖方法plot()

本篇內(nèi)容介紹了“如何使用matplotlib中的折線圖方法plot()”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁(yè)制作,對(duì)成都茶藝設(shè)計(jì)等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。

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'] # 會(huì)按順序【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>)

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

如何使用matplotlib中的折線圖方法plot()

print('x軸刻度標(biāo)簽:',list(xticklabels_lst))  #是個(gè)文本標(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] # 會(huì)按數(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>)

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

如何使用matplotlib中的折線圖方法plot()

print('x軸刻度標(biāo)簽:',list(xticklabels_lst))  #是個(gè)按序號(hào)排列的文本標(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>)

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

如何使用matplotlib中的折線圖方法plot()

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

#返回一個(gè)空列表  plt.plot()

[]

如何使用matplotlib中的折線圖方法plot()

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

1個(gè)參數(shù)

#提供一個(gè)數(shù)(點(diǎn))  plt.plot(4.5,marker='o')

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

如何使用matplotlib中的折線圖方法plot()

#提供一個(gè)數(shù)字序列  plt.plot([4.5,2,3],marker='o')

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

如何使用matplotlib中的折線圖方法plot()

2個(gè)參數(shù)

自動(dòng)解析位置參數(shù)的原則

(x,y)形式

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

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

如何使用matplotlib中的折線圖方法plot()

# x/y 為標(biāo)量  plt.plot(2,['z'],marker = 'o')

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

如何使用matplotlib中的折線圖方法plot()

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

如何使用matplotlib中的折線圖方法plot()

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

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

如何使用matplotlib中的折線圖方法plot()

3個(gè)參數(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>]

如何使用matplotlib中的折線圖方法plot()

# fmt不寫,或者&lsquo;&rsquo;,則使用默認(rèn)樣式  plt.plot([2,1,3],[0.5,2,2.5],'',  #          marker='o'           markersize = 15          )

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

如何使用matplotlib中的折線圖方法plot()

繪圖Line2D

僅畫線:繪圖的默認(rèn)情況

默認(rèn)樣式:藍(lán)色的【線】【無標(biāo)記】

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

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

如何使用matplotlib中的折線圖方法plot()

僅畫標(biāo)記

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

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

如何使用matplotlib中的折線圖方法plot()

畫線+標(biāo)記

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

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

如何使用matplotlib中的折線圖方法plot()

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

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

如何使用matplotlib中的折線圖方法plot()

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],每個(gè)都是可選的,每個(gè)屬性可以選擇寫或者不寫  # 而且與組合中它們所在的位置順序無關(guān)

如何使用matplotlib中的折線圖方法plot()

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

如何使用matplotlib中的折線圖方法plot()

標(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>]

如何使用matplotlib中的折線圖方法plot()

綜合:帶有空心圓標(biāo)記的線條圖

'''  標(biāo)記點(diǎn)是覆蓋在線條的上面,位于上層  圖層層次:[top]  spines > marker > line > backgroud  [bottom]  spines:軸的4個(gè)邊框  spines 將線條圖圍在里面  '''  plt.plot([1,5,3,4],            marker = 'o',           markersize = 20,           # edge           markeredgecolor = 'r',           markeredgewidth = 5,           # face            markerfacecolor = 'w',    # 白色,與背景色相同,把線條覆蓋著,營(yíng)造空心的視覺效果  #          markerfacecolor = 'none', # 無色,透明,會(huì)看到線條  #          markerfacecolor = None, # 不設(shè)置,默認(rèn)顏色          )  # markerfacecolor = ' ', # 無法識(shí)別  # markerfacecolor = '', # 無法識(shí)別

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

如何使用matplotlib中的折線圖方法plot()

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

如何使用matplotlib中的折線圖方法plot()

DataFrame數(shù)據(jù)

#DataFrame數(shù)據(jù)  d = {'name':list('abcd'),'age':[22,20,18,27]}  df = pd.DataFrame(d) df
 nameage
0a22
1b20
2c18
3d27
plt.plot('name','age',data = df)

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

如何使用matplotlib中的折線圖方法plot()

“如何使用matplotlib中的折線圖方法plot()”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章題目:如何使用matplotlib中的折線圖方法plot()
網(wǎng)頁(yè)路徑:http://bm7419.com/article12/igogdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、商城網(wǎng)站用戶體驗(yàn)、網(wǎng)頁(yè)設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)