简体   繁体   English

Matplotlib - 表面错误地绘制在线条的顶部

[英]Matplotlib - Surface incorrectly plotted on top of Lines

I'm trying to have a graph with plotted lines above a displayed image surface, however any lines drawn appear underneath the image.我正在尝试在显示的图像表面上方绘制带有绘制线的图形,但是绘制的任何线条都出现在图像下方。 I'm using plot_surface to display the image as in the example function display_image_on_z_plane below.我正在使用plot_surface显示图像,如下面的示例 function display_image_on_z_plane所示。 There should be a line across the X axis here.这里应该有一条横穿 X 轴的线。

Edit: I have tried placing the call before/after the drawing of the line.编辑:我尝试在画线之前/之后拨打电话。 Using Matplotlib 3.5.0.使用 Matplotlib 3.5.0。 Plotting a surface above a surface seems to work fine, ala Add background image to 3d plot , but not this.在表面上方绘制表面似乎工作正常,ala Add background image to 3d plot ,但不是这个。

Am I doing something wrong or is this a bug?我做错了什么还是这是一个错误? Or is there another way to get around this?还是有其他方法可以解决这个问题?

Thanks谢谢

在此处输入图像描述

    import matplotlib.pyplot as plt
    from matplotlib import cm
    import numpy as np
    import cv2
    
    def display_image_on_z_plane(image, ax, z_level):
        step_x, step_y = 10. / image.shape[0], 10. / image.shape[1]
    
        bg_X = np.arange(-5., 5., step_x)
        bg_Y = np.arange(-5., 5., step_y)
    
        bg_X, bg_Y = np.meshgrid(bg_X, bg_Y)
    
        ax.plot_surface(bg_X, bg_Y, np.atleast_2d(z_level), rstride=2, cstride=2, facecolors=image, shade=False)
    
    fig = plt.figure()
    ax = plt.axes(projection='3d')
    
    # Display Random Graph
    
    X = [-5, 5]
    Y = [0, 0]
    Z = [0, 0]
    ax.plot(X, Y, Z)
    
    
    image = cv2.imread("Lenna_(test_image).png")
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image.astype(np.float32) / 255.0
    image = cv2.flip(image, 0)
    
    display_image_on_z_plane(image, ax, -0.01)
    ax.set_zlim(-0.05, 0.3)
    plt.show()

Apparently this is due to the way Matplotlib works.显然这是由于 Matplotlib 的工作方式造成的。 You have to manually specify the z order.您必须手动指定 z 顺序。

Here is my updated code example:这是我更新的代码示例:

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import cv2

def display_image_on_z_plane(image, ax, z_level):
    step_x, step_y = 10. / image.shape[0], 10. / image.shape[1]

    bg_X = np.arange(-5., 5., step_x)
    bg_Y = np.arange(-5., 5., step_y)

    bg_X, bg_Y = np.meshgrid(bg_X, bg_Y)

    ax.plot_surface(bg_X, bg_Y, np.atleast_2d(z_level), rstride=2, cstride=2, facecolors=image, shade=False, zorder=0)

fig = plt.figure()
ax = plt.axes(projection='3d')

image = cv2.imread("Lenna_(test_image).png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.astype(np.float32) / 255.0
image = cv2.flip(image, 0)

display_image_on_z_plane(image, ax, -0.01)

# Display Random Graph

X = [-5, 5]
Y = [0, 0]
Z = [0, 0]
ax.plot(X, Y, Z, zorder=10)


ax.set_zlim(-0.05, 0.3)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM