python (matplotlib)画三维图像

关于三维图像的内容很多博友已经写了
推荐:三维绘图,画三维图,3d图-英文版
上面写的都非常详细,很推荐,特别是英文版那个,基于此,只给我写的一个例子

三维图

f ( x , y ) = x 2 + y 2 f(x,y)=x^2+y^2 f(x,y)=x2+y2的三维图
python (matplotlib)画三维图像_第1张图片

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维不相关高斯分布的3维图,即下面公式中n=2的情况
python (matplotlib)画三维图像_第2张图片
python (matplotlib)画三维图像_第3张图片

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的简写

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

#设置2维表格
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')
# 显示等高线图
#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')

python (matplotlib)画三维图像_第4张图片

2维等高线

将上面的图截取截面就是2维平面,是一个个圆形
python (matplotlib)画三维图像_第5张图片

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的简写

#定义坐标轴函数
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轴,经过x=0的点
    ax.axis["y"] = ax.new_floating_axis(1, 0)
    ax.axis["y"].set_axisline_style("-|>", size=1.5)
#    第一条线,x轴,经过y=0的点
    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)

#设置画布
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')
#在已经定义好的画布上加入高斯函数
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)

你可能感兴趣的:(python,matplotlib)