赞
踩
目录
- #%%
- #1.绘制三维坐标轴的方法
- from matplotlib import pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- #开始绘图
- #采用方法1或者方法2都能够建立三维坐标轴
- fig=plt.figure()
- """
- 就个人而言还是比较推荐方法1的.
- 因为再使用方法二的时候,某些函数再jupyter中不能够运行,最后显示的图片是空白甚至报错!
- """
- ax1=plt.axes(projection='3d')#方法1
- ax2=Axes3D(fig)#方法2
- plt.xlabel("x")
- plt.ylabel("y")
- plt.title("A blank 3D axis")
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/空白三维轴.png', dpi=500, bbox_inches='tight')
- fig = plt.figure() #定义新的三维坐标轴
- ax3 = plt.axes(projection='3d')
-
- #定义三维数据
- xx = np.arange(-5,5,0.5)
- yy = np.arange(-5,5,0.5)
- X, Y = np.meshgrid(xx, yy)
- Z = np.sin(X)+np.cos(Y)
-
- #作图
- ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap='rainbow')
- plt.xlabel("x")
- plt.ylabel("y")
- plt.title("3D surface plot")
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/三维曲面.png', dpi=500, bbox_inches='tight')
- plt.show()
- #绘制等高线曲面tu
- from matplotlib import pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
-
- #定义坐标轴
- fig4 = plt.figure()
- ax4 = plt.axes(projection='3d')
-
- #生成三维数据
- xx = np.arange(-5,5,0.1)
- yy = np.arange(-5,5,0.1)
- X, Y = np.meshgrid(xx, yy)
- Z = np.cos(np.sqrt(X**2+Y**2))
-
- #作图,分别向对应的面做投影线,增强效果美感
- ax4.plot_surface(X,Y,Z,alpha=0.3,cmap='winter') #生成表面, alpha 用于控制透明度
- ax4.contour(X,Y,Z,zdir='z', offset=-3,cmap="rainbow") #生成z方向投影,投到x-y平面
- ax4.contour(X,Y,Z,zdir='x', offset=-6,cmap="rainbow") #生成x方向投影,投到y-z平面
- ax4.contour(X,Y,Z,zdir='y', offset=6,cmap="rainbow") #生成y方向投影,投到x-z平面
- #设定显示范围
- ax4.set_xlabel('X')
- ax4.set_xlim(-6, 4)
- ax4.set_ylabel('Y')
- ax4.set_ylim(-4, 6)
- ax4.set_zlabel('Z')
- ax4.set_zlim(-3, 3)
- plt.title("Contour surface plot")
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/等高线图.png', dpi=500, bbox_inches='tight')
- plt.show()
- #再绘制一个3D彩面图
- %matplotlib inline
- %config InlineBackend.figure_format = 'retina'
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
-
- # x,y的导入
- x=np.linspace(-3,3,1000)
- y=np.linspace(-3,3,1000)
- x, y = np.meshgrid(x,y)
- z = (1 - x / 2 + x ** 5 + y ** 3) * \
- np.exp(-(x *x+y *y))
-
- # 绘制图片
- fig = plt.figure()
- plt.title("3D Light Surface", fontsize=18)
- #ax3d = Axes3D(fig) #写第一种图片总是出不来,我们一般推荐第种写法
- ax3d = mp.axes(projection="3d") # 同样可以实现
- ax3d.set_xlabel("X",color='r')
- ax3d.set_ylabel("Y",color='r')
- ax3d.set_zlabel("Z",color='r')
-
- plt.tick_params(labelsize=10)
- ax3d.plot_surface(x, y, z, rstride=20,cstride=20, cmap="jet")
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/3D彩色图片.png', dpi=500, bbox_inches='tight')
- plt.show()
- #再画一个利用coolwarm类型的图
- import pylab as plt
- import numpy as np
- #数据处理
- X=np.linspace(-6,6,1000)
- Y=np.linspace(-6,6,1000)
- X,Y=np.meshgrid(X,Y)
- #设置绘图
- #推荐plt.axes的写法,不容易出现图像显示空白的情况
- ax=plt.axes(projection="3d")
-
- Z=np.sin(np.sqrt(X*X+Y*Y))
-
- surf=ax.plot_surface(X,Y,Z,cmap="coolwarm")
- plt.colorbar(surf)
- ax.set_xlabel("X",color='r')
- ax.set_ylabel("Y",color='r')
- plt.title("3D CoolWarm Surface", fontsize=10)
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/3D CoolWarm Surface.png', dpi=500, bbox_inches='tight')
- plt.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
-
- #数据处理
- X1=np.linspace(-6,6,2000)
- Y1=np.linspace(-6,6,2000)
- x,y=np.meshgrid(X1,Y1)
- #代入计算行数
- def gaussian_fun(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)
- mu_x=0
- mu_y=0
- sigma_x=0.8
- sigma_y=0.8
- F_x_y = gaussian_fun(X,Y,mu_x,mu_y,sigma_x,sigma_y)
- #可视化处理
- fig = plt.figure()
- ax = plt.axes(projection='3d')
- ax.plot_surface(X,Y,F_x_y,cmap='jet')
- # 显示等高线图
- plt.xlabel("x",color='r')
- plt.ylabel("y",color='r')
- plt.title("Gaussian Function Figure",color='r')
- plt.savefig('C:/Users/Zeng Zhong Yan/Desktop/Gaussian Function Figure.png', dpi=500, bbox_inches='tight')
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。