【问题标题】:create 3d surface graph without all the points创建没有所有点的 3d 曲面图
【发布时间】:2020-05-31 03:45:23
【问题描述】:

我有一些 (X, Y, Z) 数据点,我想为其制作曲面图。 X,Y,Z 是整数。 X 范围从 1 到 6,Y 范围从 1 到 6。我有除 (5,6) 和 (6,6) 之外的所有 (X,Y) 点的 Z 数据。所以我必须为这两点设置 Z = 0。有没有办法让我不只为这两个点绘制图表,而是为所有其他点绘制图表?代码如下:

import matplotlib.pyplot as plt
import numpy as np

v = [
    (1,1,1),
    (1,2,1),
    (1,3,1),
    (1,4,1),
    (1,5,1),
    (1,6,1),
    (2,1,2),
    (2,2,3),
    (2,3,4),
    (2,4,4),
    (2,5,5),
    (2,6,6),
    (3,1,3),
    (3,2,4),
    (3,3,4),
    (3,4,4),
    (3,5,4),
    (3,6,5),
    (4,1,4),
    (4,2,4),
    (4,3,4),
    (4,4,4),
    (4,5,5),
    (4,6,6),
    (5,1,5),
    (5,2,5),
    (5,3,5),
    (5,4,5),
    (5,5,6),
    (6,1,6),
    (6,2,5),
    (6,3,5),
    (6,4,5),
    (6,5,6),
]

def f(x, y):
    for xt,yt,z in v:
        if xt == x and yt == y:
            return z
    return 0


x = np.arange(1, 7, 1)
y = np.arange(1, 7, 1)

# X, Y = np.meshgrid(x, y)
# print(X)
# print(Y)

X = [
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6]
]
X = np.array(X)

Y = [
    [1, 1, 1, 1, 1, 1],
    [2, 2, 2, 2, 2, 2],
    [3, 3, 3, 3, 3, 3],
    [4, 4, 4, 4, 4, 4],
    [5, 5, 5, 5, 5, 5],
    [6, 6, 6, 6, 6, 6]
]
Y = np.array(Y)

Z = []
for yi in y:
    Z.append([])
    for xi in x:
        Z[yi-1].append(f(xi, yi))
Z = np.array(Z)

# print(Z)


ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
ax.set_title('surface')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

这是我得到的情节:

我想删除我必须为点 (5,6) 和 (6,6) 放置的 0

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    不幸的是,matplotlib 中的 plot_surface 方法忽略了掩码(请参阅this open issue),因此您需要使用 np.nan 代替 0 以使这些特定的 Z 值不显示。这不能完美地工作,并且可能导致像颜色图这样的渲染伪影无法正常工作 - 但您可以看到曲面图显示除了点 (X,Y) = (5,6), (6,6) .

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    
    v = [
        (1,1,1),
        (1,2,1),
        (1,3,1),
        (1,4,1),
        (1,5,1),
        (1,6,1),
        (2,1,2),
        (2,2,3),
        (2,3,4),
        (2,4,4),
        (2,5,5),
        (2,6,6),
        (3,1,3),
        (3,2,4),
        (3,3,4),
        (3,4,4),
        (3,5,4),
        (3,6,5),
        (4,1,4),
        (4,2,4),
        (4,3,4),
        (4,4,4),
        (4,5,5),
        (4,6,6),
        (5,1,5),
        (5,2,5),
        (5,3,5),
        (5,4,5),
        (5,5,6),
        (6,1,6),
        (6,2,5),
        (6,3,5),
        (6,4,5),
        (6,5,6),
    ]
    
    def f(x, y):
        for xt,yt,z in v:
            if xt == x and yt == y:
                return z
        return np.nan
    
    
    x = np.arange(1, 7, 1)
    y = np.arange(1, 7, 1)
    
    # X, Y = np.meshgrid(x, y)
    # print(X)
    # print(Y)
    
    X = [
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 4, 5, 6]
    ]
    X = np.array(X)
    
    Y = [
        [1, 1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3, 3],
        [4, 4, 4, 4, 4, 4],
        [5, 5, 5, 5, 5, 5],
        [6, 6, 6, 6, 6, 6]
    ]
    Y = np.array(Y)
    
    Z = []
    for yi in y:
        Z.append([])
        for xi in x:
            Z[yi-1].append(f(xi, yi))
    # Z = np.array(Z)
    # prepare the masked array
    Z = np.ma.array(Z)
    Z_masked = np.ma.masked_where(Z == np.nan, Z)
    # print(Z)
    
    
    ax = plt.axes(projection='3d')
    ax.plot_surface(X, Y, Z_masked, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
    ax.set_title('surface')
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()
    

    【讨论】:

    • 当我运行您的代码时,它在图表中没有显示任何内容,也没有进行渲染。并且程序给出了这个警告:code.py:89: UserWarning: Z contains NaN values。这可能会导致渲染伪影。 ax.plot_surface(X, Y, Z_masked, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
    • 我得到了同样的警告,但图形呈现。你用的是什么版本的 matplotlib?
    • 我在 Linux Mint 19.3 上使用 matplotlib 版本:3.2.1,numpy 版本:1.18.4,python 版本:3.6.9
    • 可能是因为 Linux 操作系统。无论如何,谢谢你让我知道这是不可能的。
    • 很高兴能帮上一点忙!也许 seaborn 或 plotly 将能够创建缺少点的曲面图
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2019-10-02
    • 2015-09-13
    • 2014-05-03
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多