1

i have a problem plotting polygons using matplotlib in 3D. Under some circumstances there always seems to occur some kind of graphical error where polygons are shown that are covered by other polygons. This results in some really weird locking plots. But i can't figure out, where i make an error in the code. Maybe yome of you have had the problem befor and already a solution for it. My example code is as follows:

from mpl_toolkits.mplot3d import Axes3D       
from mpl_toolkits.mplot3d.art3d import Poly3DCollection                                 
import matplotlib.pyplot as plt                                                         
import numpy as np                                                                      
                                                                                          
                                                                                          
z = np.zeros((10,10))                                                                   
z[5,5] = 5                                                                              
z[4,5] = 2                                                                              
z[4,4] = 2.8                                                                            
                                                                                          
nx,ny = (10,10)                                                                         
xv = np.linspace(0,9,nx)                                                                
yv = np.linspace(0,9,ny)                                                                
x,y = np.meshgrid(xv,yv)                                                                
y = np.flipud(y)                                                                        
                                                                                          
                                                                                          
fig = plt.figure()                                                                      
ax = Axes3D(fig)                                                                        
ax.set_xlim3d(np.min(np.array(x)),np.max(np.array(x)))
ax.set_ylim3d(np.min(np.array(y)),np.max(np.array(y)))                                                                     
ax.set_zlim3d(np.min(np.array(z)),np.max(np.array(z)))                                  
ax.view_init(elev=45,azim=0)                                                            
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))                                           
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))                                           
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))                                           
ax.xaxis._axinfo["grid"]['color'] =  (1,1,1,0)                                          
ax.yaxis._axinfo["grid"]['color'] =  (1,1,1,0)                                          
ax.zaxis._axinfo["grid"]['color'] =  (1,1,1,0)                                          
ax.set_axis_off()                                                                       
                                                                                          
for d1 in range(ny-1):                                                                  
    for d2 in range(nx-1):                                                              
        xp = [x[d1,d2],x[d1+1,d2],x[d1,d2+1]]
        yp = [y[d1,d2],y[d1+1,d2],y[d1,d2+1]]                                                                                                     
        zp = [z[d1,d2],z[d1+1,d2],z[d1,d2+1]]                                           
        verts = [list(zip(xp,yp,zp))]                                                   
        ax.add_collection3d(Poly3DCollection(verts,facecolor='mediumblue',              
                                  linewidths=1,edgecolor='black'))                      
        xp = [x[d1+1,d2],x[d1+1,d2+1],x[d1,d2+1]]                                       
        yp = [y[d1+1,d2],y[d1+1,d2+1],y[d1,d2+1]]                                       
        zp = [z[d1+1,d2],z[d1+1,d2+1],z[d1,d2+1]]                                       
        verts = [list(zip(xp,yp,zp))]                                                   
        tri = ax.add_collection3d(Poly3DCollection(verts,facecolor='goldenrod',         
                                  linewidths=1,edgecolor='black'))                      
                                                                                          
plt.savefig('out.png') 

A figure that shows the problem can be seen here, have a look at the 6th column from the left, close to the middle of the plot. These error seems to be realted to the angle, in ohter azimut angles this error does not occur. But it is not a solution to change the azimuth angle because the such errors can occur in other position. Does someone know what i have done wrong and how to do it right? enter image description here

1 Answer 1

1

If you rotate, you'll see that inconsistent depth rendering is the issue:

enter image description here

This is unfortunately a known issue that is even addressed in the Matplotlib FAQ:

My 3D plot doesn’t look right at certain viewing angles This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look “physically correct.”

Unfortunately, while some work is being done to reduce the occurrence of this artifact, it is currently an intractable problem, and can not be fully solved until matplotlib supports 3D graphics rendering at its core.

If you read on, their official recommendation is to use Mayavi for the time being. It's probably best to follow this recommendation if you require a larger amount of flexibility. Otherwise, you will probably have to stick to certain angles that work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that really fast and astonishing great answer :) I still had the fear that it was something like this. using only specific angles is not an option, because i want to plot orography with more thousends and thousends of polygons. So there will be very likely problems like this at every possible angle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.