0

I have many triangles (say N=10^6) with (x,y,z) coordinates of each vertex of the triangles stored in a file. So each triangle has 9 numbers stored as a row in the file. Hence the file has N rows. Now I just want to plot (in 3d) all the triangles filled with some colour. The triangles may or may not be adjacent. I am very very confused surfing through matplotlib documentation. Kindly help. Don't scold me please.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
deltasata
  • 377
  • 1
  • 4
  • 21

1 Answers1

4

Plotting 10 million triangles on a plot which has at most 1 million pixels may not make too much sense. In any case, if you do not have information about which vertex is adjacent to which other, you cannot directly use the plot_trisurf method.

I see two options:

  1. Plot a Poly3DCollection.
  2. Filter the unique points from the data and supply those to plot_trisurf. Using this method, you may not be able to colorize the triangles to your wishes, but only according to z-Value.

The following would be an example on how to plot a Poly3DCollection from your input data. For the purpose of demonstration we first need to provide some sample data (this needs to be the duty of the questioner, not the answerer).

import numpy as np
np.set_printoptions(threshold='nan')

phi = np.linspace(0,2*np.pi, 7)
x = np.cos(phi) + np.sin(phi)
y = -np.sin(phi) + np.cos(phi)
z = np.cos(phi)*0.12+0.7

a = np.zeros((len(phi)-1, 9))
a[:,0] = x[:-1]
a[:,1] = y[:-1]
a[:,2] = z[:-1]
a[:,3:6] = np.roll( a[:,0:3], -1, axis=0)
a[:,8] = np.ones_like(phi[:-1])
a = np.around(a, 2)
print a

which prints

[[ 1.    1.    0.82  1.37 -0.37  0.76  0.    0.    1.  ]
 [ 1.37 -0.37  0.76  0.37 -1.37  0.64  0.    0.    1.  ]
 [ 0.37 -1.37  0.64 -1.   -1.    0.58  0.    0.    1.  ]
 [-1.   -1.    0.58 -1.37  0.37  0.64  0.    0.    1.  ]
 [-1.37  0.37  0.64 -0.37  1.37  0.76  0.    0.    1.  ]
 [-0.37  1.37  0.76  1.    1.    0.82  0.    0.    1.  ]]

(every set of 3 columns belongs to one point, first column is x, second y, third, z).

Now we can actually build the Poly3Dcollection.

from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

fc = ["crimson" if i%2 else "gold" for i in range(a.shape[0])]

poly3d = [[ a[i, j*3:j*3+3] for j in range(3)  ] for i in range(a.shape[0])]

ax.add_collection3d(Poly3DCollection(poly3d, facecolors=fc, linewidths=1))

ax.set_xlim(-1.5,1.5)
ax.set_ylim(-1.5,1.5)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • This really helps. But as the no of triangles increases the plotting becomes very slow and ram consuming. I noticed the same in matlab. I think I can not circumvent this problem. Lets see. Thanks again. – deltasata Apr 13 '17 at 18:00
  • One major problem I found with the above code: when I comment out `ax.set_xlim()` to set autoscale it does not work. Somehow the plot range becomes (0,1) for all three axes when I do not explicitly set the range. Please help. – deltasata Apr 13 '17 at 18:17
  • So my question is, in the above code, how to set auto x,y,z limit. This is very important because, for huge no of triangles, the code is taking huge time to generate the plot. So it is very time consuming to first plot the triangles with full range and then see and adjust the xlim, ylim and zlim. – deltasata Apr 13 '17 at 18:37
  • As I said, i wouldn't consider plotting so many triangles to make any sense, as you will end up with more triangles than points on the screen. In order to set the limits, you can simply find out the minimum and maximum from the data and use them as limits. – ImportanceOfBeingErnest Apr 13 '17 at 21:17
  • I agree. But 10^6 is maximum no of triangles I may have. I need the code also for smaller no of triangles, say 1lakh. I also wanted to know if I can set the viewing angle while saving the fig as pdf. As you know, the triangle no is so high I can not rotate the fig from plt.show() and then save. So i need to give the viewing angle before saving the fig in the code itself. Please help. – deltasata Apr 14 '17 at 10:37
  • Questions on stackoverflow are about specific problems. Here you asked about plotting triangles, and received an answer. If you want to know about setting limits or setting the viewing angle (which can be done using `ax.view_init()`) you should first search for a possible solution and if you cannot fine any or if it does not give the desired result, ask a new specific question about it. – ImportanceOfBeingErnest Apr 14 '17 at 10:53