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)
F_x_y = gaussian(X,mu_x,sigma_x)
#显示2d等高线图,画100条线
# plt.contour(X,Y,F_x_y,100)
# fig.show()
#显示三维图
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')
fig.show()

=======================二维========================

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)
#F_x_y = gaussian(X,mu_x,sigma_x)
#显示2d等高线图,画100条线
plt.contour(X,Y,F_x_y,100)
fig.show()

圆形

python gaussian,gaussian2-LMLPHP

矩形:

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)
F_x_y = gaussian(X,mu_x,sigma_x)
#显示2d等高线图,画100条线
plt.contour(X,Y,F_x_y,100)
fig.show()

  python gaussian,gaussian2-LMLPHP

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()

python gaussian,gaussian2-LMLPHP

05-14 01:55