python pyplot的plot( )函數


函數原型

plt.plot(x,y,format_string,**kwargs)
  • x:x軸數據,列表或數組,可選
  • y:y軸數據,列表或數組
  • format_string:控制曲線的格式字符串,可選
  • **kwargs:其實就是可以畫多條曲線的意思,追加(,x,y,format_string)就可以了

1、有無 x 軸數據示例

import numpy as np
import matplotlib.pyplot as plt

fig1 = plt.figure(num=1,figsize=(7,5))
x = np.linspace(0.0,np.pi*2,12)
y = np.sin(x)
plt.plot(x,y,'go:')     # 指定x軸數據
fig2 = plt.figure(num=2)
plt.plot(y,'rx-')       # 無x軸數據
plt.show()

在這里插入圖片描述
無 x 軸數據則默認把 y 軸數據的下標當成 x 軸

2、畫多條曲線

import numpy as np
import matplotlib.pyplot as plt

fig1 = plt.figure(num=1,figsize=(7,5))
x = np.linspace(0.0,np.pi*2,20)
y = np.sin(x)
plt.plot(x, y,'rx-', x,2*x,'go-.')     # 每條都指定x軸數據
fig2 = plt.figure(num=2)
plt.plot(x, y,'rx-',2*x,'go-.')       # 一條指定x軸數據,其他不指定
fig2 = plt.figure(num=3)
plt.plot(y,'rx-',2*x,'go-.')     # 都不指定
plt.show()

figure1

figure1
figure2

在這里插入圖片描述

figure3
在這里插入圖片描述
3、format_string:控制曲線的格式字符串,可選,由顏色字符、風格字符和標記字符組成。

顏色字符 說明 顏色字符 說明 顏色字符 說明
‘r’ 紅色 ‘g’ 綠色 ‘b’ 藍色
‘c’ 青綠色 ‘k’ 黑色 ‘y’ 黃色
‘w’ 白色 ‘m’ 洋紅色
風格字符 說明
‘-‘ 實線
‘–’ 破折線
‘-.’ 點畫線
‘:’ 虛線
標記字符 說明 標記字符 說明 標記字符 說明
‘.’ 點標記 ‘,’ 像素標記 ‘o’ 實心圈標記
‘v’ 倒三角標記 ‘^’ 上三角標記 ‘>’ 右三角標記
‘<’ 左三角標記 ‘h’ 豎六邊形標記 ‘H’ 橫六邊形標記
‘+’ 十字標記 ‘x’ x標記 ‘D’ 菱形標記
‘d’ 瘦菱形標記 ‘|’ 垂直線標記 ‘*’ 星形標記
‘p’ 實心五角標記 ’s’ 實心方形標記 ‘4’ 右花三角標記
‘3’ 左花三角標記 ‘2’ 上花三角標記 ‘1’ 下花三角標記

使用方式:

format_string 是三個的組合

plt.plot(x, y,'rx-') 
plt.plot(x, y,'go-.') 

或者只有其中兩個或一個

plt.plot(x, y,'w:') 
plt.plot(x, y,'y') 

都可以

或者說其他方式控制線性顏色

color : 控制顏色,color=’green’

linestyle : 線條風格,linestyle=’dashed’

marker : 標記風格,marker = ‘o’

markerfacecolor : 標記顏色,markerfacecolor = ‘blue’

markersize : 標記尺寸,markersize = ‘20’

使用方式

plt.plot(x, y,color='green')

在這里插入圖片描述

plt.plot(x, y,color='green',marker = 'o',markerfacecolor = 'blue') 

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM