您的位置:首页技术文章
文章详情页

python matplotlib绘制三维图的示例

【字号: 日期:2022-07-10 09:52:12浏览:4作者:猪猪

作者:catmelo 本文版权归作者所有

链接:https://www.cnblogs.com/catmelo/p/4162101.html

本文参考官方文档:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

起步

新建一个matplotlib.figure.Figure对象,然后向其添加一个Axes3D类型的axes对象。其中Axes3D对象的创建,类似其他axes对象,只不过使用projection=’3d’关键词。

import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = fig.add_subplot(111, projection=’3d’)

3D曲线图

python matplotlib绘制三维图的示例

import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib.pyplot as pltmpl.rcParams[’legend.fontsize’] = 10fig = plt.figure()ax = fig.gca(projection=’3d’)theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)z = np.linspace(-2, 2, 100)r = z**2 + 1x = r * np.sin(theta)y = r * np.cos(theta)ax.plot(x, y, z, label=’parametric curve’)ax.legend()ax.set_xlabel(’X Label’)ax.set_ylabel(’Y Label’)ax.set_zlabel(’Z Label’)plt.show()

简化用法:

python matplotlib绘制三维图的示例

from pylab import *from mpl_toolkits.mplot3d import Axes3Dplt.gca(projection=’3d’)plt.plot([1,2,3],[3,4,1],[8,4,1],’--’)plt.xlabel(’X’)plt.ylabel(’Y’)#plt.zlabel(’Z’) #无法使用

3D散点图

python matplotlib绘制三维图的示例

import numpy as npfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltdef randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) + vminfig = plt.figure()ax = fig.add_subplot(111, projection=’3d’)n = 100for c, m, zl, zh in [(’r’, ’o’, -50, -25), (’b’, ’^’, -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zl, zh) ax.scatter(xs, ys, zs, c=c, marker=m)ax.set_xlabel(’X Label’)ax.set_ylabel(’Y Label’)ax.set_zlabel(’Z Label’)plt.show()

以上就是matplotlib绘制三维图的示例的详细内容,更多关于matplotlib绘制三维图的资料请关注好吧啦网其它相关文章!

标签: Python 编程
相关文章: