一、Matplotlib基礎
下面的測試數據下載
Matplotlib中的基本圖表包括的元素
1) x軸和y軸 axis
水平和垂直的軸線
2)軸標籤 axisLabel
水平和垂直的軸標籤
3)x軸和y軸刻度 tick
刻度標示座標軸的分隔,包括最小刻度和最大刻度
4)x軸和y軸刻度標籤 tick label
表示特定座標軸的值
5)繪圖區域(座標系) axes
實際繪圖的區域
6)畫布 figure
呈現所有的座標系
1)只含單一曲線的圖
畫一條正弦曲線
x = np.arange(-np.pi,np.pi,0.1) # [start,] stop[, step,] 開始值 結束值 步長值
x
y = np.sin(x)
y
plt.plot(x,y) # plot(x, y) 最簡單的參數形式 傳入x和y的值
2)只含多個曲線的圖
方法一:使用多個plot函數(推薦)
x1 = np.arange(-np.pi,0,0.1)
x2 = np.arange(0,np.pi,0.1)
# 方式一:多次使用 plt.plot繪製多個函數
plt.plot(x1,np.sin(x1))
plt.plot(x2,np.sin(x2))
方法二:在一個plot函數中傳入多對X,Y值
x1 = np.arange(-np.pi,0,0.1)
x2 = np.arange(0,np.pi,0.1)
#調用一次plt.plot裏面傳入多個x和y的值 plt.plot(x1,y1,x2,y2,...xn,yn)
plt.plot(x1,np.sin(x1),x2,np.sin(x2))
3)子畫布
matplotlib中畫布分爲主畫布與子畫布,主畫布包含子畫布
1、設置主畫布的大小
plt.figure(figsize=None)
x = np.arange(-np.pi,np.pi,0.1)
plt.figure(figsize=(5,5)) #設置主畫布大小 注意設置畫布大小必須在圖形之前纔有效
plt.plot(x,np.tan(x))
2、設置子畫布的大小
plt.subplot(*args, **kwargs)
'''
# subplot(2, 3, 3)
創建一個子畫布 把原來的大的畫布
上下分成2部分 左右分成3部分 佔用哪個部分
'''
(1) : 創建子畫布
plt.figure(figsize=(8,8))
axes1 = plt.subplot(4,2,1) # 根據傳入的參數創建子畫布 返回座標系對象這裏佔縱向的1/4
axes2 = plt.subplot(4,2,4)
axes3 = plt.subplot(2,2,3) #注意,這裏的縱向畫布是佔總畫布的一半
axes4 = plt.subplot(2,2,4)
(2) : 在子畫布中繪圖
在每一個小的座標系上繪圖 而不是在大的畫布上繪圖
axes1.plot(x,np.sin(x))
axes2.plot(x,np.cos(x))
axes3.plot(x,np.tan(x))
axes4.plot(x,np.tanh(x))
4)網格線
plt.grid(b=None,which=’major’,axis=’both’,**kwargs)
注意:如果畫布有子畫布的話,plt.grid只會給離他最近的那個子畫布繪製網格線
一般給子畫布單獨繪製網格線
創建一個空的畫布
plt.figure(figsize=(8,8))
axes1 = plt.subplot(221)
axes2 = plt.subplot(222)
axes3 = plt.subplot(223)
axes4 = plt.subplot(224)
axis顯示軸向
'''
axis: 默認爲橫縱都有
axis="x" 在 X 軸上方繪製網格線
axis="y" 在 Y 軸上方繪製網格線
'''
axes2.grid(axis='x') # 在軸上方繪製網格線
color(c)代表顏色
alpha表示線的明暗程度
linewidth(lw)代表線的粗細
axes4.grid(color='red',linewidth=2,alpha=0.2)
axes1.grid() # axis='both' 默認水平豎直方向都有網格線
axes2.grid(axis='x') # 在軸上方繪製網格線
axes3.grid(axis='y')
axes4.grid(color='red',linewidth=2,alpha=0.2)
5)座標軸的軸線
plt.axis([xmin,xmax,ymin,ymax])
1)設置座標軸的刻度起止值
(1) plt.axis()設置座標軸起始值
plt.axis([xmin,xmax,ymin,ymax])
x = np.linspace(-1,1,100)
# x**2+y**2=1
# y**2=1-x**2
# y=(1-x**2)**0.5
y=(1-x**2)**0.5
plt.plot(x,y)
plt.plot(x,-y)
plt.axis([-2,2,-2,2]) # [x軸的起始值,x軸的結束值,y軸的起始值,y軸的結束值]
(2) xlim方法和ylim方法設置座標軸起始值
2)設置座標軸類型
plt.axis(‘xxx’) ‘off’、’equal’……
'''
equal:改變*x*或*y*軸的極限,使其等於*x*的增量
和*y*長度相同;一般用來繪製圓形
off:隱藏座標軸
'''
(2) 繪製圓形
x = np.linspace(-1,1,100)
6)座標軸的標籤
設置座標軸標籤的方向,顏色,旋轉角度
plt.xlabel( )方法 和 plt.ylabel( )方法
plt.xlabel(s,color=’orange’,fontsize=20,rotation=20)
'''
s: 標籤名稱
color:標籤顏色
fontsize:標籤字體大小
rotation: 標籤旋轉角度
'''
# 正弦曲線
x = np.arange(-np.pi,np.pi,0.1)
y = np.sin(x)
plt.plot(x,y)
# X Y 標籤的內容
plt.xlabel('x')
plt.ylabel('f(x)=sin(x)')
plt.xlabel('x',color='orange',fontsize=20,rotation=20) #設置X軸標籤的內容及樣式
plt.ylabel('f(x)=sin(x)',rotation=60 ,fontsize=20) #設置Y軸標籤的內容及樣式
7)畫布的標題
plt.title(s, *args, **kwargs)
'''
color:顏色
fontsize:字體大小
rotation:旋轉角度
loc:位置{'center', 'left', 'right'}
默認在中間
'''
# 正弦曲線
x = np.arange(-np.pi,np.pi,0.1)
x
y = np.sin(x)
plt.plot(x,y)
plt.title('Sin(x)',color='red',fontsize=25,rotation=45,loc='right') # 默認在中間
8)圖例
(1)方式一 plt.plot()中傳入名字
# 方式一 plt.plot()中傳入名字
x = np.linspace(0,10,101)
x
plt.plot(x,x,label='normal')
plt.plot(x,2*x,label='fast')
plt.plot(x,x/2,label='slow')
plt.legend() #注意,使用label之後需要調用legend()
(2)方式二 plt.plot()中傳入名字
x = np.linspace(0,10,101)
x
plt.plot(x,x)
plt.plot(x,2*x)
plt.plot(x,x/2)
plt.legend(['normal','fast','slow'])
(3) loc參數
功能:設置圖例的位置
默認在最優位置
字符串 | 數值 | 字符串 | 數值 |
---|---|---|---|
best 最優的位置 | 0 | center left 中間靠左 | 6 |
upper right 右上角 | 1 | center right 中間靠右 | 7 |
upper left 坐上角 | 2 | lower center 中間靠下 | 8 |
lower left 左下角 | 3 | upper center 中間靠上 | 9 |
lower right | 右下角 | center 正中間 | 10 |
right 中間靠右 | 5 |
data = np.random.randint(0,100,size=(10,3))
df = DataFrame(data,columns=list('ABC'))
df
plt.plot(df['A'])
plt.plot(df['B'])
plt.plot(df['C'])
plt.legend(['1','2','3'],loc=9)
loc參數還可以是2個元素的列表,表示圖例左下角的座標
- [0,0] 左下
- [0,1] 左上
- [1,0] 右下
- [1,1] 右上
data = np.random.randint(0,100,size=(10,3))
df = DataFrame(data,columns=list('ABC'))
df
plt.plot(df['A'])
plt.plot(df['B'])
plt.plot(df['C'])
plt.legend(['1','2','3'],loc=[1,0.5])
(4) ncol參數
功能:設置圖例有幾列
data = np.random.randint(0,100,size=(10,3))
df = DataFrame(data,columns=list('ABC'))
df
plt.plot(df['A'])
plt.plot(df['B'])
plt.plot(df['C'])
plt.legend(ncol=3)
二、設置plot的風格和樣式
1、點和線的樣式
(1)顏色
參數colo=”red”或c=”red”
顏色值的方式
1.合法的HTML顏色名
2.別名
3.HTML十六進制字符串
4.歸一化到[0, 1]的RGB元組
x = np.linspace(-np.pi,np.pi,100)
x
plt.plot(x,np.sin(x),color='red') #常規設置顏色
plt.plot(x,np.sin(x),c='red') #簡寫設置顏色
plt.plot(x,np.sin(x),c='#aabbcc') #十六進制設置顏色
plt.plot(x,np.sin(x),c='r') #十六進制設置顏色
plt.plot(x,np.sin(x),c=(1,0.2,0)) #RGB設置顏色
2)透明度plt.plot(x,y,alpha=0.2)
設置線條的顏色
x = np.linspace(-np.pi,np.pi,100)
plt.plot(x,np.sin(x),c=(1,0.2,0.5),alpha=0.5) # 取值範圍0-1
3)背景色plt.subplot(facecolor=”顏色”)
注意:
設置背景色,通過plt.subplot()方法傳入facecolor參數
先畫背景色 再劃線 否則背景色就把線給蓋住了
"""
facecolor:設置背景顏色
facecolor="green" 可以直接傳入顏色
facecolor=(0,1,0) 可以傳入RGB三色
"""
x = np.linspace(-np.pi,np.pi,100)
plt.subplot(facecolor=(0,0,1))
plt.plot(x,np.sin(x),c=(1,0,0))
4)線型和線寬
(1)線型 參數linestyle或ls
線條風格 | 描述 | 線條風格 | 描述 |
---|---|---|---|
‘-‘ | 實線 | ‘:’ | 虛線 |
‘–’ | 破折線 | ‘steps’ | 階梯線 |
‘-.’ | 點劃線 | ‘None’ / ‘,’ | 什麼都不畫 |
(2)寬度 參數linewidth或lw
x = np.arange(0,10,1)
plt.plot(x,x,ls='--',lw=3) #ls設置線型 lw設置線寬
5)破折線(斷續線)
設置破折號序列各段的寬度
plt.plot(x,y,dashes=[10,10])
'''
dashes = [5, 5] #長度5,後面空5
dashes = [2, 3, 5,1] #長度2,後面空3, 長度5, 空1
'''
x = np.arange(-np.pi,np.pi,0.1)
y = np.sin(x)
plt.plot(x,y,dashes=[2,3,5,1])
6) 點型
在繪圖時,是將連續的點連起來的圖形
這是將點顯示出來
'''
marker 設置點形
markersize 設置點形大小
'''
下面是一些點的形狀
標記 | 描述 | 標記 | 描述 |
---|---|---|---|
‘1’ | 一角朝下的三腳架 | ‘3’ | 一角朝左的三腳架 |
‘2’ | 一角朝上的三腳架 | ‘4’ | 一角朝右的三腳架 |
’s’ | 正方形 | ‘p’ | 五邊形 |
‘h’ | 六邊形1 | ‘H’ | 六邊形2 |
‘8’ | 八邊形 | ||
‘.’ | 點 | ‘x’ | X |
‘*’ | 星號 | ‘+’ | 加號 |
‘,’ | 像素 | ||
‘o’ | 圓圈 | ‘D’ | 菱形 |
‘d’ | 小菱形 | ’ ’ , ‘None’ , ’ ‘,None |
無 |
‘_’ | 水平線 | ‘|’ | 豎線 |
‘v’ | 一角朝下的三角形 | ‘<’ | 一角朝左的三角形 |
‘^’ | 一角朝上的三角形 | ‘>’ | 一角朝右的三角形 |
x = np.arange(1,10,1)
y = np.sin(x)
plt.plot(x,y,marker='*',markersize=20)
7)點線設置參數總結
參數 | 描述 | 參數 | 描述 |
---|---|---|---|
color或c | 線的顏色 | linestyle或ls | 線型 |
linewidth或lw | 線寬 | marker | 點型 |
markeredgecolor | 點邊緣的顏色 | markeredgewidth | 點邊緣的寬度 |
markerfacecolor | 點內部的顏色 | markersize | 點的大小 |
2、同時設置多個曲線樣式
1)多曲線同一設置
多個曲線樣式相同
x = np.linspace(0,100,30)
# plt.plot(x1,y1,x2,y2,x3,y3...) 顏色 點的形狀 線的樣式
plt.plot(x,x,x,2*x,x,x/2,c='r',marker='*',ls=':')
2)多個曲線不同設置
多個都進行設置時,多參數連用 plt.plot(x1, y1, fmt1, x2, y2, fmt2, …)
'''
fmt = '[color][marker][line]' 顏色 點的樣式 線的樣式
fmt format 格式 格式化字符串 就是按照人家指定的格式去設置樣式的字符串
'''
x = np.linspace(0,100,30) # 0-100要30個
plt.plot(x,x,'ro:',x,2*x,'k*--',x,x/2,'c1-.') #[color][marker][line]' '顏色 點的樣式 線的樣式'
3、曲線樣式的三種設置方式
(1)對座標系使用一系列的setter方法
x = np.linspace(0,100,30) # 0-100要30個
x
axes = plt.subplot() #獲取座標系
axes.plot(x,x,x,2*x,x,x/2) #畫三條線
axes.set_title('title') #設置標題
axes.set_facecolor('gray') #設置背景色
axes.set_xlabel('x') #設置X軸標籤
axes.set_ylabel('y') #設置Y軸標籤
(2)對線使用一系列的setter方法
plt.plot()方法返回一個包含所有線的列表
設置每一個線需要獲取該線對象
x = np.linspace(0,100,30) # 0-100要30個
x
lines = plt.plot(x,x,x,2*x,x,x/2) #返回一個包含所有線的列表對象
lines[0].set_linewidth(5) #設置第一條線的寬度
lines[1].set_linestyle(':') #設置第二條線的樣式
lines[2].set_color('y') #設置第三條先線的顏色
4、X、Y軸座標刻度
1)plt.xticks()和plt.yticks()方法
需指定刻度值和刻度名稱 plt.xticks([刻度起始值],[名稱列表])
x = np.linspace(-np.pi,np.pi,100)
plt.plot(x,np.sin(x))
plt.xticks([-np.pi,0,np.pi],['-π',0,'π']) #設置x軸軸線的刻度
plt.yticks([-1,0,1]) #設置Y軸
plt.grid() #畫網格線 網格線是按照刻度來畫的
使用LaTex語法,用 、 等表達式在圖表上寫上希臘字母
x = np.linspace(-np.pi,np.pi,100)
plt.plot(x,np.sin(x))
plt.xticks([-3.14,-3.14/2,0,3.14/2,3.14],["-$\pi$","-$\pi$/2",0,"$\pi$/2","$\pi$"])
plt.xticks([-3.14,-3.14/2,0,3.14/2,3.14],["-$\pi$","-$\pi$/2",0,"$\pi$/2","$\pi$"],fontsize=20,color="red",rotation=20)
2)plt.set__xticks()和plt.yticks()方法
5、網格線密度
網格線是由X軸和Y軸的值決定的。
x = np.arange(-np.pi, np.pi, step=0.1)
plt.plot(x,np.sin(x))
plt.xticks(np.arange(-4,4,0.5)) #密度大
# plt.xticks(np.arange(-4,4,1)) #密度小
plt.grid()
三、2D圖形
1、直方圖
plt.hist(x,bins=None,normed=False,color=”blue”,orientation=”vertical”,**kwargs)
'''
bins: 整數值,也可以是表示bin的一個序列。默認值爲10
density:將表示個數的值變成表示密度的值
color: 直方圖的顏色
orientation: 默認爲vertical 豎直直方圖
'''
data = np.random.randint(0,100,size=100)
data
plt.hist(data, bins=5, density=True, color="red", orientation="horizontal")
# bins=5 將所有的數據分爲5份
# density=True 將表示有多少的數據轉變爲數據的密度
# orientation="horizontal" 水平直方圖
2、條形圖
條形圖適合比較各部分的大小
# height 豎直方向的長度 width 水平方向的長度
1)豎直方向plt.bar()
# x = index
data = np.array([1,4,7,2,5,8,9])
index = [1,2,3,4,5,6,7]
plt.bar(x=index,height=data,width=0.5)
注意豎直方向與水平方向上的height和width是相反的
2)水平方向plt.barh()
# y = index
data = np.array([1,4,7,2,5,8,9])
index = [1,2,3,4,5,6,7]
plt.barh(y=index,height=0.5,width=data)
3)極座標plt.axes(polar=True)
在平面內取一個定點O,叫極點,引一條射線Ox,叫做極軸,再選定一個長度單位和角度的正方向(通常取逆時針方向)。對於平面內任何一點M,用ρ表示線段OM的長度(有時也用r表示),θ表示從Ox到OM的角度,ρ叫做點M的極徑,θ叫做點M的極角,有序數對 (ρ,θ)就叫點M的極座標,這樣建立的座標系叫做極座標系。
data = np.array([1,4,7,2,5,8,9])
index = [1,2,3,4,5,6,7]
plt.axes(polar=True) # 極座標
plt.bar(x=index,height=data,width=0.5)
3、餅圖
餅圖適合展示各部分佔總體的比例
1)普通各部分佔滿餅圖
x = [89,45,32,16]
plt.pie(x)
2)普通各部分未佔滿餅圖
x = [0.1,0.2,0.25,0.23]
plt.pie(x)
3)餅圖各參數
'''
labels:設置每一塊的標籤; 必須一一對應,不能是中文
labeldistance:設置標籤距離圓心的距離(0爲 圓餅中心數 1爲圓餅邊緣)
autopct:設置比例值的顯示格式(%1.1f%%)保留一位小數的百分比;
pctdistance:設置比例值文字距離圓心的距離(浮點值)
explode:設置每一塊頂點距圓心的長度(比例值,列表);
colors:設置每一塊的顏色(列表);最好一一對應
shadow:設置是否繪製陰影,默認爲False
startangle:設置餅圖起始角度 正右方爲0度
'''
x = [89,45,32,16]
plt.pie(x,
labels=['A','B','C','D'],
labeldistance=1.2,
autopct="%1.1f%%", #1.1f% 保留一位小數的百分比
pctdistance=0.5, # 比例值文字距離圓心的距離
explode=[0,0.2,0,0], # 每一塊頂點距圓形的長度
colors=["red","blue","yellow","green"], #最好一一對應
shadow=True, # 是否有陰影
startangle=60 #第一塊開始的角度
4、散點圖plt.scatter()
plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)
'''
s=None :大小 可以傳入多個值 間隔輸出
c=None :點的顏色 可以傳入多個值(可以傳入多個列表【多維數組】,也可以傳入多個元祖) 間隔輸出
marker :點的形狀
cmap :顏色映射
alpha :透明度
'''
x = np.random.normal(loc=0,scale=1,size=1000)
y = np.random.normal(loc=0,scale=1,size=1000)
plt.scatter(x, y)
plt.scatter(x, y,s = [30, 100],c=[(1,0,0),(0,1,0),(0,0,1)],marker="o",alpha=0.8)
隨機散點圖
x = np.random.normal(loc=0,scale=1,size=1000)
y = np.random.normal(loc=0,scale=1,size=1000)
size = np.random.randint(10,100,size=1000) #隨機產生1000個整數
color = np.random.random(size=(1000,3)) #隨機給1000個整數產生 RGB
plt.scatter(x, y, s=size, c=color ,marker="o", alpha=0.8)
=============以上爲重點=================
四、圖形內的文字、註釋、箭頭
1)圖形內的文字
pyplot函數 | API方法 | 描述 |
---|---|---|
text() | mpl.axes.Axes.text() | 在Axes對象的任意位置添加文字 |
xlabel() | mpl.axes.Axes.set_xlabel() | 爲X軸添加標籤 |
ylabel() | mpl.axes.Axes.set_ylabel() | 爲Y軸添加標籤 |
title() | mpl.axes.Axes.set_title() | 爲Axes對象添加標題 |
legend() | mpl.axes.Axes.legend() | 爲Axes對象添加圖例 |
figtext() | mpl.figure.Figure.text() | 在Figure對象的任意位置添加文字 |
suptitle() | mpl.figure.Figure.suptitle() | 爲Figure對象添加中心化的標題 |
annnotate() | mpl.axes.Axes.annotate() | 爲Axes對象添加註釋(箭頭可選) |
(1)圖形內的文字
plt.suptitle("圖形") #整個圖形的標題
plt.suptitle("suptitle") #整個圖形的標題
plt.figtext(0.5,0.5,"figtext") #必須傳入x y來設置文本位置 在整個圖中,按照位置設置文字
(2)座標內的文字
axes = plt.subplot()
plt.suptitle("suptitle") #整個圖形的標題
plt.figtext(0.5,0.5,"figtext") #必須傳入x y來設置文本位置 在整個圖中,按照位置設置文字
axes.text(0.2,0.2,'axes text') #必須傳入x y來設置文本位置 座標軸內設置文本
axes.set_title('axes title') # 給座標軸設置title
2)註釋plt.annotate( )
'''
s:要註釋的內容
xy:設置箭頭指示的位置
xytext:設置註釋文字的(左下角)位置
arrowprops:以字典的形式設置箭頭的樣式
width:設置箭頭長方形部分的寬度
headlength:設置箭頭尖端的長度,
headwidth:設置箭頭尖端底部的寬度
shrink:設置箭頭頂點、尾部與指示點、註釋文字的距離(比例值),可以理解爲控制箭頭的長度
'''
data = np.random.randint(0,10,size=10)
index = np.arange(0,10,1)
plt.plot(index, data)
plt.annotate(s="this is a annotate", xy=[5,4], xytext=[3,8],arrowprops={"width":2,'shrink':0.6})
3)箭頭
4)綜合運用
庫裏面還定義了一些arrowstyle可以選擇的風格樣式
'->'
head_length=0.4,head_width=0.2
'-['
widthB=1.0,lengthB=0.2,angleB=None
'|-|'
widthA=1.0,widthB=1.0
'-|>'
head_length=0.4,head_width=0.2
'<-'
head_length=0.4,head_width=0.2
'<->'
head_length=0.4,head_width=0.2
'<|-'
head_length=0.4,head_width=0.2
'<|-|>'
head_length=0.4,head_width=0.2
'fancy'
head_length=0.4,head_width=0.4,tail_width=0.4
'simple'
head_length=0.5,head_width=0.5,tail_width=0.2
'wedge'
tail_width=0.3,shrink_factor=0.5
這裏不同 arrowprops={"arrowstyle":'->'}
plt.plot(index, data)
plt.annotate(s="this is a annotate", xy=[5,4], xytext=[3,8],arrowprops={"arrowstyle":'->'})
五、3D圖
1) 導包
from mpl_toolkits.mplot3d.axes3d import Axes3D
2) 使用mershgrid函數切割x,y軸
功能: 將二維座標轉換爲三維座標, 將座標網格化
(1) 創建座標軸所需數據
x = np.linspace(0,10,101)
y = np.linspace(0,10,101)
x,y
(array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,
5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5,
6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6,
7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7,
8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8,
9.9, 10. ]),
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,
5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5,
6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6,
7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7,
8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8,
9.9, 10. ]))
(2) mershgrid函數切割x,y軸
功能:切割後能被3D座標系使用, 將x, y 軸轉換爲X, Y軸爲3D座標系使用
X,Y = np.meshgrid(x,y)
X,Y
(array([[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ],
[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ],
[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ],
...,
[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ],
[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ],
[ 0. , 0.1, 0.2, ..., 9.8, 9.9, 10. ]]),
array([[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0.1, 0.1, 0.1, ..., 0.1, 0.1, 0.1],
[ 0.2, 0.2, 0.2, ..., 0.2, 0.2, 0.2],
...,
[ 9.8, 9.8, 9.8, ..., 9.8, 9.8, 9.8],
[ 9.9, 9.9, 9.9, ..., 9.9, 9.9, 9.9],
[10. , 10. , 10. , ..., 10. , 10. , 10. ]]))
3)創建3d座標系
功能: 建立一個三維座標系, 返回一個三維座標
axes = plt.subplot(projection=’3d’)
4)繪製3d圖形
數據: 使用上邊的X, Y
'''
參數:
cmap='rainbow' 將圖形顯示爲彩色
'''
Z = np.sin(X)-np.cos(Y)
從X矩陣中對應位置 取值 求sin 然後減去 從Y矩陣中對應位置 取值 求cos
得到的值 是 Z矩陣中對應位置的值
axes = plt.subplot(projection='3d')
axes.plot_surface(X,Y,Z)
5)添加colorbar
功能: 爲圖形添加狀態條
plt.colorbar(p,shrink=0.5)
'''
參數
p 3D圖形對象
shrink 改變進度條的長度
注意: 3D圖形的顏色需要時可表示的顏色纔可以使用colorbar(顏色全是藍色的就不能設置)
'''
axes = plt.subplot(projection='3d')
p = axes.plot_surface(X,Y,Z,cmap='rainbow')
plt.colorbar(p,shrink=0.8)
6)綜合繪製3D圖
plt.figure(figsize=(10,6)) #改變畫布大小
axes = plt.subplot(projection='3d') ##
p = axes.plot_surface(X,Y,Z,cmap='rainbow')
plt.colorbar(p,shrink=0.5)
六、極座標(玫瑰)圖
創建極座標,設置 polar 屬性 plt.axes(polar = True)
1)普通數字繪製極座標
data = np.array([2,4,6,8,10,12])
index = np.array([1,2,3,4,5,6])
plt.axes(polar=True)
plt.bar(x=index, height=data)
2)π 繪製極座標
index = np.arange(0,2*np.pi,2*np.pi/8) #將0-2π 分爲8分
plt.axes(polar=True)
plt.bar(x=index,height=[1,2,3,4,5,6,7,8])
3)角度和弧度的轉換
如果數據用的是角度(0-360的)
極座標用的是弧度(0度-360度)
弧度/(2*np.pi)=角度/360
弧度=角度*np.pi/180
plt.axes(polar=True)
plt.bar(x=角度數據*np.pi/180,height= 實際數據)