如何使用matplotlib在指定半径的点(x1,y1)和(x2,y2)的长度上绘制圆柱体? [英] How to draw a cylinder using matplotlib along length of point (x1,y1) and (x2,y2) with specified radius?

查看:31
本文介绍了如何使用matplotlib在指定半径的点(x1,y1)和(x2,y2)的长度上绘制圆柱体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 matplotlib 沿着点 (x1,y1) 和 (x2,y2) 的长度绘制一个具有指定半径 r 的圆柱体.请让我知道该怎么做.

解决方案

只是为了好玩,我将把它推广到任何轴 (x0, y0, z0) 到 (x1, y1, z1).如果要在xy平面上放置轴,请将z0和z1设置为0.

通过找到与轴相同方向的单位向量,然后将其添加到 p0 并沿轴的长度缩放,您可以很容易地找到轴的向量方程.通常你可以找到 x = x0 + cos(theta) * R 和 y = y0 + sin(theta) * R 的圆的坐标,但是这些圆不在 xy 平面中,所以我们需要使我们自己的轴的单位矢量垂直于圆柱体的轴并且彼此垂直,然后从中获取xyz坐标.我使用这个网站来帮助我解决这个问题:

I would like to draw a cylinder using matplotlib along length of point (x1,y1) and (x2,y2) with specified radius r. Please let me know how to do this.

解决方案

Just for fun, I'm going to generalize this to any axis (x0, y0, z0) to (x1, y1, z1). Set z0 and z1 to 0 if you want an axis in the xy plane.

You can find a vector equation for the axis pretty easily by finding the unit vector in the same direction as the axis, then adding it to p0 and scaling it along the length of the axis. Normally you can find the coordinates of a circle with x = x0 + cos(theta) * R and y = y0 + sin(theta) * R, but the circles aren't in the xy plane, so we're going to need to make our own axes with unit vectors perpendicular to the axis of the cylinder and each other and then get the xyz coordinates from that. I used this site to help me figure this out: http://mathforum.org/library/drmath/view/51734.html. Here's the code:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.linalg import norm

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
origin = np.array([0, 0, 0])
#axis and radius
p0 = np.array([1, 3, 2])
p1 = np.array([8, 5, 9])
R = 5
#vector in direction of axis
v = p1 - p0
#find magnitude of vector
mag = norm(v)
#unit vector in direction of axis
v = v / mag
#make some vector not in the same direction as v
not_v = np.array([1, 0, 0])
if (v == not_v).all():
    not_v = np.array([0, 1, 0])
#make vector perpendicular to v
n1 = np.cross(v, not_v)
#normalize n1
n1 /= norm(n1)
#make unit vector perpendicular to v and n1
n2 = np.cross(v, n1)
#surface ranges over t from 0 to length of axis and 0 to 2*pi
t = np.linspace(0, mag, 100)
theta = np.linspace(0, 2 * np.pi, 100)
#use meshgrid to make 2d arrays
t, theta = np.meshgrid(t, theta)
#generate coordinates for surface
X, Y, Z = [p0[i] + v[i] * t + R * np.sin(theta) * n1[i] + R * np.cos(theta) * n2[i] for i in [0, 1, 2]]
ax.plot_surface(X, Y, Z)
#plot axis
ax.plot(*zip(p0, p1), color = 'red')
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
plt.show()

这篇关于如何使用matplotlib在指定半径的点(x1,y1)和(x2,y2)的长度上绘制圆柱体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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