龙空技术网

是时候抛弃MATLAB了,用Python绘制三维图形更方便!

大数据研习社 6624

前言:

如今你们对“python画图形”都比较关怀,各位老铁们都需要知道一些“python画图形”的相关知识。那么小编在网摘上网罗了一些对于“python画图形””的相关知识,希望兄弟们能喜欢,我们快快来了解一下吧!

Python提供了强大的绘图工具包matplotlib,不仅可以绘制常规图形,还可以绘制3D图形,绘制3D图像还需要再安装mpl_toolkits工具包。

以下简单展示几个利用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()plt.show()
三维散点图
import numpy as npfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as plt#定义了一个生成随机数的函数def 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()
三维表面图
fig = plt.figure()ax = fig.gca(projection='3d')#绘图数据源X = np.arange(-5, 5, 0.25)Y = np.arange(-5, 5, 0.25)X, Y = np.meshgrid(X, Y)#meshgrid 函数用来生成网格矩阵,可以是二维网格矩阵,也可以是三维。R = np.sqrt(X**2 + Y**2)Z = np.sin(R)#函数sin(sqrt(x**2+Y**2))surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))fig.colorbar(surf, shrink=0.5, aspect=5)plt.show()
三维球面
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport numpy as npfig = 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()
莫比乌斯带
import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.tri as mtri# u, v are parameterisation variablesu = (np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) * np.ones((10, 1))).flatten()v = np.repeat(np.linspace(-0.5, 0.5, endpoint=True, num=10), repeats=50).flatten()# This is the Mobius mapping, taking a u, v pair and returning an x, y, z# triplex = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)z = 0.5 * v * np.sin(u / 2.0)# Triangulate parameter space to determine the trianglestri = mtri.Triangulation(u, v)fig = plt.figure()ax = fig.add_subplot(1, 1, 1, projection='3d')# The triangles in parameter space determine which x, y, z points are# connected by an edgeax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)ax.set_zlim(-1, 1)# First create the x and y coordinates of the points.n_angles = 36n_radii = 8min_radius = 0.25radii = np.linspace(min_radius, 0.95, n_radii)angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)angles[:,1::2] += np.pi/n_anglesx = (radii*np.cos(angles)).flatten()y = (radii*np.sin(angles)).flatten()z = (np.cos(radii)*np.cos(angles*3.0)).flatten()# Create the Triangulation; no triangles so Delaunay triangulation created.triang = mtri.Triangulation(x, y)# Mask off unwanted triangles.xmid = x[triang.triangles].mean(axis=1)ymid = y[triang.triangles].mean(axis=1)mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)triang.set_mask(mask)plt.show()

感谢观看,喜欢的朋友,关注走一波,后续内容更精彩!

标签: #python画图形