1. 程式人生 > 其它 >matplotlib.pyplot.plot()引數詳解

matplotlib.pyplot.plot()引數詳解

https://matplotlib.org/api/pyplot_summary.html

https://matplotlib.org/api/pyplot_summary.html

 

在互動環境中檢視幫助文件:

import matplotlib.pyplot as plt
help(plt.plot)

以下是對幫助文件重要部分的翻譯:

plot函式的一般的呼叫形式:

#單條線:
plot([x], y, [fmt], data=None, **kwargs)
#多條線一起畫
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

  

可選引數[fmt] 是一個字串來定義圖的基本屬性如:顏色(color),點型(marker),線型(linestyle),

具體形式  fmt = '[color][marker][line]'

fmt接收的是每個屬性的單個字母縮寫,例如:

plot(x, y, 'bo-')  # 藍色圓點實線

  

若屬性用的是全名則不能用*fmt*引數來組合賦值,應該用關鍵字引數對單個屬性賦值如:

plot(x,y2,color='green', marker='o', linestyle='dashed', linewidth=1, markersize=6)

plot(x,y3,color='#900302',marker='+',linestyle='-')

常見的顏色引數:**Colors**
也可以對關鍵字引數color賦十六進位制的RGB字串如 color='#900302'

 

    =============    ===============================
    character        color
    =============    ===============================
    ``'b'``          blue 藍
    ``'g'``          green 綠
    ``'r'``          red 紅
    ``'c'``          cyan 藍綠
    ``'m'``          magenta 洋紅
    ``'y'``          yellow 黃
    ``'k'``          black 黑
    ``'w'``          white 白
    =============    ===============================

  點型引數**Markers**,如:marker='+' 這個只有簡寫,英文描述不被識別

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

  線型引數**Line Styles**,linestyle='-'

    =============    ===============================
    character        description
    =============    ===============================
    ``'-'``          solid line style 實線
    ``'--'``         dashed line style 虛線
    ``'-.'``         dash-dot line style 點畫線
    ``':'``          dotted line style 點線
    =============    ===============================

  樣例1

函式原型:matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
>>> plot('xlabel', 'ylabel', data=obj)
解釋:All indexable objects are supported. This could e.g. be a dict, a pandas.DataFame or a structured numpy array.
data 引數接受一個物件資料型別,所有可被索引的物件都支援,如 dict 等

  

import matplotlib.pyplot as plt  
import numpy as np
'''read file 
fin=open("para.txt")
a=[]
for i in fin:
  a.append(float(i.strip()))
a=np.array(a)
a=a.reshape(9,3)
'''
a=np.random.random((9,3))*2 #隨機生成y
 
y1=a[0:,0]
y2=a[0:,1]
y3=a[0:,2]
 
x=np.arange(1,10)
 
ax = plt.subplot(111)
width=10
hight=3
ax.arrow(0,0,0,hight,width=0.01,head_width=0.1, head_length=0.3,length_includes_head=True,fc='k',ec='k')
ax.arrow(0,0,width,0,width=0.01,head_width=0.1, head_length=0.3,length_includes_head=True,fc='k',ec='k')
 
ax.axes.set_xlim(-0.5,width+0.2)
ax.axes.set_ylim(-0.5,hight+0.2)
 
plotdict = { 'dx': x, 'dy': y1 }
ax.plot('dx','dy','bD-',data=plotdict)
 
ax.plot(x,y2,'r^-')
ax.plot(x,y3,color='#900302',marker='*',linestyle='-')
 
plt.show()

  

樣例2,

import matplotlib.pyplot as plt  
import numpy as np  
  
x = np.arange(0, 2*np.pi, 0.02)  
y = np.sin(x)  
y1 = np.sin(2*x)  
y2 = np.sin(3*x)  
ym1 = np.ma.masked_where(y1 > 0.5, y1)  
ym2 = np.ma.masked_where(y2 < -0.5, y2)  
  
lines = plt.plot(x, y, x, ym1, x, ym2, 'o')  
#設定線的屬性
plt.setp(lines[0], linewidth=1)  
plt.setp(lines[1], linewidth=2)  
plt.setp(lines[2], linestyle='-',marker='^',markersize=4)  
#線的標籤
plt.legend(('No mask', 'Masked if > 0.5', 'Masked if < -0.5'), loc='upper right')  
plt.title('Masked line demo')  
plt.show()  

  

 

 

 

 

import numpy as np
import matplotlib.pyplot as plt
 
theta = np.arange(0, 2*np.pi, 0.01)
xx = [1,2,3,10,15,8]
yy = [1,-1,0,0,7,0]
rr = [7,7,3,6,9,9]
 
fig = plt.figure()
axes = flg.add_subplot(111)
 
i = 0
while i < len(xx):
    x = xx[i] + rr[i] *np.cos(theta)
    x = xx[i] + rr[i] *np.cos(theta)
    axes.plot(x,y)
    axes.plot(xx[i], yy[i], color='#900302', marker='*')
     i = i+1
width = 20
hight = 20
axes.arrow(0,0,0,hight,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
axes.arrow(0,0,width,0,width=0.01,head_width=0.1,head_length=0.3,fc='k',ec='k')
plt.show()