關(guān)于三維圖像的內(nèi)容很多博友已經(jīng)寫了
推薦:三維繪圖,畫三維圖,3d圖-英文版
上面寫的都非常詳細(xì),很推薦,特別是英文版那個(gè),基于此,只給我寫的一個(gè)例子
三維圖
畫
f ( x , y ) = x 2 + y 2 f(x,y)=x^2+y^2
f
(
x
,
y
)
=
x
2
+
y
2
的三維圖
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
from
mpl_toolkits
.
mplot3d
import
Axes3D
x
=
np
.
arange
(
-
10
,
10
,
0.2
)
y
=
np
.
arange
(
-
10
,
10
,
0.2
)
f_x_y
=
np
.
power
(
x
,
2
)
+
np
.
power
(
y
,
2
)
fig
=
plt
.
figure
(
)
ax
=
plt
.
gca
(
projection
=
'3d'
)
ax
.
plot
(
x
,
y
,
f_x_y
)
畫出2維不相關(guān)高斯分布的3維圖,即下面公式中n=2的情況
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
from
mpl_toolkits
.
mplot3d
import
Axes3D
#畫三維圖不可少
from
matplotlib
import
cm
#cm 是colormap的簡(jiǎn)寫
# 1_dimension gaussian function
def
gaussian
(
x
,
mu
,
sigma
)
:
f_x
=
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)
)
*
np
.
exp
(
-
np
.
power
(
x
-
mu
,
2
.
)
/
(
2
*
np
.
power
(
sigma
,
2
.
)
)
)
return
(
f_x
)
# 2_dimension gaussian function
def
gaussian_2
(
x
,
y
,
mu_x
,
mu_y
,
sigma_x
,
sigma_y
)
:
f_x_y
=
1
/
(
sigma_x
*
sigma_y
*
(
np
.
sqrt
(
2
*
np
.
pi
)
)
**
2
)
*
np
.
exp
(
-
np
.
power\
(
x
-
mu_x
,
2
.
)
/
(
2
*
np
.
power
(
sigma_x
,
2
.
)
)
-
np
.
power
(
y
-
mu_y
,
2
.
)
/
\
(
2
*
np
.
power
(
sigma_y
,
2
.
)
)
)
return
(
f_x_y
)
#設(shè)置2維表格
x_values
=
np
.
linspace
(
-
5
,
5
,
2000
)
y_values
=
np
.
linspace
(
-
5
,
5
,
2000
)
X
,
Y
=
np
.
meshgrid
(
x_values
,
y_values
)
#高斯函數(shù)
mu_x
,
mu_y
,
sigma_x
,
sigma_y
=
0
,
0
,
0.8
,
0.8
F_x_y
=
gaussian_2
(
X
,
Y
,
mu_x
,
mu_y
,
sigma_x
,
sigma_y
)
#顯示三維圖
fig
=
plt
.
figure
(
)
ax
=
plt
.
gca
(
projection
=
'3d'
)
ax
.
plot_surface
(
X
,
Y
,
F_x_y
,
cmap
=
'jet'
)
# 顯示等高線圖
#ax.contour3D(X,Y,F_x_y,50,cmap='jet')
三維等高線
將上面等高線打開,三維圖注釋掉
#ax.plot_surface(X,Y,F_x_y,cmap='jet')
# 顯示等高線圖
ax
.
contour3D
(
X
,
Y
,
F_x_y
,
50
,
cmap
=
'jet'
)
2維等高線
將上面的圖截取截面就是2維平面,是一個(gè)個(gè)圓形
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
from
mpl_toolkits
.
mplot3d
import
Axes3D
#畫三維圖不可少
from
matplotlib
import
cm
#cm 是colormap的簡(jiǎn)寫
#定義坐標(biāo)軸函數(shù)
def
setup_axes
(
fig
,
rect
)
:
ax
=
axisartist
.
Subplot
(
fig
,
rect
)
fig
.
add_axes
(
ax
)
ax
.
set_ylim
(
-
4
,
4
)
#自定義刻度
# ax.set_yticks([-10, 0,9])
ax
.
set_xlim
(
-
4
,
4
)
ax
.
axis
[
:
]
.
set_visible
(
False
)
#第2條線,即y軸,經(jīng)過x=0的點(diǎn)
ax
.
axis
[
"y"
]
=
ax
.
new_floating_axis
(
1
,
0
)
ax
.
axis
[
"y"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
# 第一條線,x軸,經(jīng)過y=0的點(diǎn)
ax
.
axis
[
"x"
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
"x"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
return
(
ax
)
# 1_dimension gaussian function
def
gaussian
(
x
,
mu
,
sigma
)
:
f_x
=
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)
)
*
np
.
exp
(
-
np
.
power
(
x
-
mu
,
2
.
)
/
(
2
*
np
.
power
(
sigma
,
2
.
)
)
)
return
(
f_x
)
# 2_dimension gaussian function
def
gaussian_2
(
x
,
y
,
mu_x
,
mu_y
,
sigma_x
,
sigma_y
)
:
f_x_y
=
1
/
(
sigma_x
*
sigma_y
*
(
np
.
sqrt
(
2
*
np
.
pi
)
)
**
2
)
*
np
.
exp
(
-
np
.
power\
(
x
-
mu_x
,
2
.
)
/
(
2
*
np
.
power
(
sigma_x
,
2
.
)
)
-
np
.
power
(
y
-
mu_y
,
2
.
)
/
\
(
2
*
np
.
power
(
sigma_y
,
2
.
)
)
)
return
(
f_x_y
)
#設(shè)置畫布
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
)
)
#建議可以直接plt.figure()不定義大小
ax1
=
setup_axes
(
fig
,
111
)
ax1
.
axis
[
"x"
]
.
set_axis_direction
(
"bottom"
)
ax1
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
#在已經(jīng)定義好的畫布上加入高斯函數(shù)
x_values
=
np
.
linspace
(
-
5
,
5
,
2000
)
y_values
=
np
.
linspace
(
-
5
,
5
,
2000
)
X
,
Y
=
np
.
meshgrid
(
x_values
,
y_values
)
mu_x
,
mu_y
,
sigma_x
,
sigma_y
=
0
,
0
,
0.8
,
0.8
F_x_y
=
gaussian_2
(
X
,
Y
,
mu_x
,
mu_y
,
sigma_x
,
sigma_y
)
#顯示三維圖
#fig = plt.figure()
#ax = plt.gca(projection='3d')
#ax.plot_surface(X,Y,F_x_y,cmap='jet')
# 顯示3d等高線圖
#ax.contour3D(X,Y,F_x_y,50,cmap='jet')
# 顯示2d等高線圖,畫8條線
plt
.
contour
(
X
,
Y
,
F_x_y
,
8
)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
