Python:如何使用 Python 绘制 3D 图形?

发布于 2024-08-22 07:29:26 字数 399 浏览 2 评论 0原文

我正在使用 matplotlib 来执行此操作

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

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')

plt.show()

现在这会构建一个在 3d 空间中水平的图形。如何使图表垂直以使其面向用户?

我想要做的是构建多个这样的垂直图,这些垂直图相隔一定距离并面向用户。

I am using matplotlib for doing this

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

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')

plt.show()

Now this builds a graph that is horizontal in the 3d space. How do I make the graph vertical so that it faces the user?

What I want to do is build multiple such vertical graphs that are separated by some distance and are facing the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無心 2024-08-29 07:29:26

英国石油公司的答案可能很好,但还有一种更简单的方法。

您当前的图形在 z 轴上是“平坦”的,这就是它是水平的原因。您希望它是垂直的,这意味着您希望它在 y 轴上“平坦”。这涉及对代码进行最微小的修改:

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

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=x, ys=[0]*len(x), zs=y, zdir='z', label='ys=0, zdir=z')
plt.show()

然后,您可以通过使用 ys 参数的不同值轻松获得多个此类图(例如,ys=[2]*len(x) 会将图略落后)。

bp's answer might work fine, but there's a much simpler way.

Your current graph is 'flat' on the z-axis, which is why it's horizontal. You want it to be vertical, which means that you want it to be 'flat' on the y-axis. This involves the tiniest modification to your code:

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

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=x, ys=[0]*len(x), zs=y, zdir='z', label='ys=0, zdir=z')
plt.show()

Then you can easily have multiple such graphs by using different values for the ys parameter (for example, ys=[2]*len(x) instead would put the graph slightly behind).

黑白记忆 2024-08-29 07:29:26

您可以使用 view_init() 函数设置 3d 图的视角。下面的示例适用于 matplotlib 1.1 版本。

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

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

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
ax.view_init(90, -90)
plt.show()

You can set the view angle of the 3d plot with the view_init() function. The example below is for version 1.1 of matplotlib.

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

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

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
ax.view_init(90, -90)
plt.show()
爱殇璃 2024-08-29 07:29:26

根据文档,您要使用 ax.plot_surface(x,y,z) 方法。更多信息和图表类型请参见此处

以下应该有效:

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)

#map data on the plane
X, Y = numpy.meshgrid(arange(0, max(x), 1), arange(0, max(y), 1))
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for x_,y_,z_ in data:
  Z[x_, y_] = z_ #this should work, but only because x and y are integers
                 #and arange was done with a step of 1, starting from 0
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_surface(X, Y, Z)

According to the documentation you want to use the ax.plot_surface(x,y,z) method. More information and chart types here.

The following should work:

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)

#map data on the plane
X, Y = numpy.meshgrid(arange(0, max(x), 1), arange(0, max(y), 1))
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for x_,y_,z_ in data:
  Z[x_, y_] = z_ #this should work, but only because x and y are integers
                 #and arange was done with a step of 1, starting from 0
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_surface(X, Y, Z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文