绘制Python 3的革命基础(也许是matplotlib) [英] Ploting solid of revolution in Python 3 (matplotlib maybe)

查看:103
本文介绍了绘制Python 3的革命基础(也许是matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候问题:

R是xy平面上由抛物线y = x ^ 2 + 1和线y = x + 3界定的区域.通过绕X轴旋转R形成旋转的实体.我需要在2D和实体旋转3D中绘制抛物线和线,这是怎么做的? 我已经安装了蟒蛇.

解决方案

您可以使用 解决方案

You could use plot_surface:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

u = np.linspace(-1, 2, 60)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

X = U
Y1 = (U**2 + 1)*np.cos(V)
Z1 = (U**2 + 1)*np.sin(V)

Y2 = (U + 3)*np.cos(V)
Z2 = (U + 3)*np.sin(V)

ax.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
plt.show()

To plot a surface using plot_surface you begin by identifying two 1-dimensional parameters, u and v:

u = np.linspace(-1, 2, 60)
v = np.linspace(0, 2*np.pi, 60)

such that x, y, z are functions of the parameters u and v:

x = x(u, v)
y = y(u, v)
z = z(u, v)

The thing to notice about ax.plot_surface is that its first three arguments must be 2D arrays. So we use np.meshgrid to create coordinate matrices (U and V) out of coordinate vectors (u and v), and define 2D arrays X, Y, Z to be functions of U and V:

X = U
Y1 = (U**2 + 1)*np.cos(V)
Z1 = (U**2 + 1)*np.sin(V)

For each location on the coordinate matrices U and V, there is a corresponding value for X and Y and Z. This creates a map from 2-dimensional uv-space to 3-dimensional xyz-space. For every rectangle in uv-space there is a face on our surface in xyz-space. The curved surface drawn by plot_surface is composed of these flat faces.

这篇关于绘制Python 3的革命基础(也许是matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆