使用matplotlib绘制3d图形

问题描述 投票:-1回答:1

我正在使用python 3.7,我试图创建一个3d图,但我无法看到图。这是我的代码:

    from mpl_toolkits.mplot3d import axes3d
    fig=matplotlib.pyplot.figure()#creating a figure
    chart=fig.add_subplot(1,1,1,projection="3d")
    X,Y,Z=[1,2,3,4,5,6,7,8],[2,5,3,8,9,5,6,1],[3,6,2,7,5,4,5,6]
    chart.plot_wireframe(X,Y,Z)
    matplotlib.pyplot.show()

enter image description here谢谢:)

python matplotlib
1个回答
2
投票

不确定要绘制的是什么,但线框的z分量必须是二维的:

这显示了一个情节:

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

fig = plt.figure()
chart = fig.add_subplot(1,1,1,projection="3d")
X, Y, Z = np.array([[1, 2, 3, 4, 5, 6, 7, 8], 
                    [2 ,5 ,3 ,8 ,9 ,5 ,6 ,1], 
                    np.array([[1, 2, 3, 4, 5, 6, 7, 8], [3, 6, 2, 7, 5, 4, 5, 6]])])
chart.plot_wireframe(X, Y, Z)
plt.show()

enter image description here


相反,如果您想绘制曲线:

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

fig = plt.figure()
chart = fig.add_subplot(1,1,1,projection="3d")
X, Y, Z = np.array([[1, 2, 3, 4, 5, 6, 7, 8], 
                    [2 ,5 ,3 ,8 ,9 ,5 ,6 ,1], 
                    [3, 6, 2, 7, 5, 4, 5, 6]])
chart.plot(X, Y, Z)
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.