【问题标题】:How to produce a revolution of a 2D plot with matplotlib in Python [duplicate]如何在 Python 中使用 matplotlib 生成 2D 图的革命[重复]
【发布时间】:2020-04-11 15:01:56
【问题描述】:

我在 Python 中使用 matplotlib 创建了一个 2D 图,例如: 它是使用 2 个列表生成的:

import matplotlib.pyplot as plt
import numpy as np

plt.plot(X, Y) #X and Y are lists, containing the x and y coordinates of points respectively
plt.show()

现在我想围绕 Y 轴创建该图的旋转,并以 Y 轴垂直的方式对其进行可视化。使用matplotlib如何做到这一点?

【问题讨论】:

  • 您想创建一个 3D 曲面作为曲线绕轴旋转的曲面吗?还是只是旋转曲线?
  • 我想要一个 3D 表面,您提供的链接很有用,但我认为 William 写的内容也非常有用并且解释得很好

标签: python matplotlib


【解决方案1】:

如果您将曲线定义为两个 1D 数组中的 xy 点的集合,并且您希望它们围绕 y 轴旋转,您只需构建 2D 数组以满足 matplotlib 的 Axes3D.plot_surface通过在 [0, 2*pi] 中使用xnp.outer()np.cos(theta)np.sin(theta)theta 获取外部产品。这将为您提供xy 空间中的笛卡尔点集合,这将表示通过围绕z 轴旋转每个原始点创建的圆。构造z 数组有点棘手,因为plot_surface() 期望的shape

这是一个完整的例子,演示了这个方法并将它与原始的 2D 绘图进行比较

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

n = 100

fig = plt.figure(figsize=(12,6))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122,projection='3d')
y = np.linspace(np.pi/8, np.pi*4/5, n)
x = np.sin(y)
t = np.linspace(0, np.pi*2, n)

xn = np.outer(x, np.cos(t))
yn = np.outer(x, np.sin(t))
zn = np.zeros_like(xn)

for i in range(len(x)):
    zn[i:i+1,:] = np.full_like(zn[0,:], y[i])

ax1.plot(x, y)
ax2.plot_surface(xn, yn, zn)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2013-04-27
    • 2021-04-05
    • 2019-09-07
    • 2018-05-17
    • 2015-09-19
    相关资源
    最近更新 更多