3D绘图顺序问题(与球体相交的线)

时间:2018-07-07 00:17:17

标签: python python-3.x matplotlib plot 3d

因此,我遇到一个问题,我想绘制一个简单的盒子,带有线框,一个彩色的面板和角上的球形。但是,尽管我设法做到了,但是球体还是有问题的,因为它们会在我喜欢的地方而不是其他地方阻塞线框。

Plot Issue (spheres intersected by lines)

任何想法为什么会发生这种情况以及如何解决此问题?

import numpy as NP
import os
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

os.system( 'cls' )


def main( ):


    fig = plt.figure( figsize = ( cm2inch( 6 ), cm2inch ( 6 ) ), dpi = 100 )
    ax = fig.add_subplot(111, projection='3d')


    # Box frame plot
    ax.plot( xs = [ 0, 0, 1, 1, 0 ], ys = [ 0, 1, 1, 0, 0 ], zs = [ 0, 0, 0, 0, 0 ], color = 'black', linewidth = 2 ) #<- Base
    ax.plot( xs = [ 0, 0, 1, 1, 0 ], ys = [ 0, 1, 1, 0, 0 ], zs = [ 1, 1, 1, 1, 1 ], color = 'black', linewidth = 2 ) #<- Top
    ax.plot( xs = [ 0, 0, 1, 1, 0 ], ys = [ 0, 0, 0, 0, 0 ], zs = [ 0, 1, 1, 0, 0 ], color = 'black', linewidth = 2 ) #<- Front
    ax.plot( xs = [ 0, 0, 1, 1, 0 ], ys = [ 1, 1, 1, 1, 1 ], zs = [ 0, 1, 1, 0, 0 ], color = 'black', linewidth = 2 ) #<- Back


    # Back face plot
    xs = [ 0, 0, 1, 1, 0 ]
    ys = [ 1, 1, 1, 1, 1 ]
    zs = [ 0, 1, 1, 0, 0 ]
    verts = [ list( zip( xs, ys, zs ) ) ]
    temp = Poly3DCollection( verts, alpha = 0.25 )
    temp.set_facecolor( 'blue' )
    ax.add_collection3d( temp )


    # Corner sphere plots
    for x in [ 0, 1 ]:
        for y in [ 0, 1 ]:
            for z in [ 0, 1 ]:
                xs, ys, zs = drawSphere( x, y, z, 0.05 )
                ax.plot_surface(xs, ys, zs, color="r", alpha = 1)

    #plt.axis('scaled')
    plt.axis( 'off' )


    plt.savefig( 'Example', pad_inches = 0, transparent = False, dpi = 400 )
    plt.show()

    plt.close( )


def drawSphere( xCenter, yCenter, zCenter, r ):
    #draw sphere
    u, v = NP.mgrid[ 0 : 2 * NP.pi : 20j, 0 : NP.pi : 10j ]
    x = NP.cos( u ) * NP.sin( v )
    y = NP.sin( u ) * NP.sin( v )
    z = NP.cos( v )  
    # shift and scale sphere
    x = r * x + xCenter
    y = r * y + yCenter
    z = r * z + zCenter
    return( x, y, z)


def cm2inch( cm ):

    inches = cm / 2.54

    return inches


if __name__ == '__main__':

    main( )

0 个答案:

没有答案