【问题标题】:How to plot the following figure?如何绘制下图?
【发布时间】:2021-10-10 08:38:32
【问题描述】:

亲爱的。我是 python 社区的初学者,我想在 python 中将下面的图片绘制为 3D。

我已经尝试过,但我无法获得任何成功的结果。 这是我的数据

这是我的尝试:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
# data
x = [86, 91, 97]
y = [82, 88, 94]
z = [80, 85, 89]
ax1.plot(x,y,z)
plt.show()

【问题讨论】:

    标签: python matplotlib 3d


    【解决方案1】:

    您的数据形状不正确。您提供 3 个平面列表,您应该提供每个条的 x、y、z 坐标。

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    import matplotlib.cm as cm
    import numpy as np
    
    # create data
    data_2d = [[729, 575, 528],
            [805, 768, 667],
            [841, 773, 724],
            [899, 857, 787]]
    
    # Convert it into an numpy array.
    data_array = np.array(data_2d)
    
    # Create a figure for plotting the data as a 3D histogram.
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # Create an X-Y mesh of the same dimension as the 2D data. The floor of the plot.
    x_data, y_data = np.meshgrid( np.arange(data_array.shape[1]),
                                  np.arange(data_array.shape[0]) )
    
    # Flatten out the arrays so that they may be passed to "ax.bar3d".
    # ax.bar3d expects three one-dimensional arrays:
    # x_data, y_data, z_data. The following call boils down to picking
    # one entry from each array and plotting a bar to from
    # (x_data[i], y_data[i], 0) to (x_data[i], y_data[i], z_data[i]).
    
    x_data = x_data.flatten()
    y_data = y_data.flatten()
    z_data = data_array.flatten()
    
    dz = z_data
    offset = dz + np.abs(dz.min())
    fracs = offset.astype(float)/offset.max()
    norm = colors.Normalize(fracs.min(), fracs.max())
    color_values = cm.jet(norm(fracs.tolist()))
    
    ax.bar3d( x_data, y_data, np.zeros(len(z_data)), 0.6, 0.6, z_data, color=color_values)
    
    # Labels
    ax.set_xlabel("Grid Size")
    ax.set_ylabel("Bézier") 
    ax.set_zlabel("Success Rate")
    # Ticks
    ax.set_zticks(range(0,1200,200))
    
    # Shape of the 3D cube 
    ax.set_box_aspect(aspect=(8,8,10))
    

    输出:

    【讨论】:

    • 非常感谢,先生。我可以像图片一样把它们分开吗?
    • 更新了答案!祝你好运!
    • 愿上帝保佑您,先生。最后一个请求,可以改变颜色吗?
    • 更新了!请注意,您需要导入几个额外的模块。
    • 非常感谢亲爱的先生。
    猜你喜欢
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2020-08-23
    相关资源
    最近更新 更多