Python Forum
How to draw rectangles in pyplot? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to draw rectangles in pyplot? (/thread-19638.html)



How to draw rectangles in pyplot? - SuchtyTV - Jul-08-2019

Hey,

from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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

zs = []
xs = []
ys = []

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

points =  [
(1.2,5,6),
(1.2,7,6),
(1.2,5,6),
(1.2,8,6),
(7,8,5),
(5,7,6),
(1,2,3),
(4,5,6),
(2,1,1),
]

rectangle =  [
((1,1,1),(2,2,2),(1,3,4)),
((2,3,4),(9,9,9),(3,4,5)),
]

for val in points:
  (x,y,z) = val
  xs.append(x)
  ys.append(y)
  zs.append(z)

for val in rectangle:
  (a,b,c) = val
  verts = [a,b,c]
  axrec.add_collection3d(Poly3DCollection(verts))

ax.scatter(xs, ys, zs, marker='o')

plt.show()
this code is supposed to draw all rectangles and points. However, I am not sure how to command the library to draw rectangles.
Thanks for your help!


RE: How to draw rectangles in pyplot? - SuchtyTV - Jul-10-2019

Hey, I am still looking for help.

Thank you!


RE: How to draw rectangles in pyplot? - SuchtyTV - Jul-14-2019

bump

Thank you!


RE: How to draw rectangles in pyplot? - scidam - Jul-14-2019

You need to be sure about points that forms a rectangle. Look at the simplest example below:

from mpl_toolkits.mplot3d import Axes3D                                                                                                                                                                                                                                                                                     
import numpy as np                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                            
from mpl_toolkits.mplot3d import Axes3D                                                                                                                                                                                                                                                                                     
from mpl_toolkits.mplot3d.art3d import Poly3DCollection                                                                                                                                                                                                                                                                     
import matplotlib.pyplot as plt                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                            
fig = plt.figure()                                                                                                                                                                                                                                                                                                          
ax = fig.add_subplot(111, projection='3d')                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                            
rects  = [[[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0 ], [0, 0, 0]]]                                                                                                                                                                                                                                                               
# Each rectangle is presented as a set of points
# array rects consist of rectangles
                                                                                                                                                                                                                                                                                                                            
ax.add_collection3d(Poly3DCollection(rects))                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                            
vv = np.array(rects)                                                                                                                                                                                                                                                                                                        
ax.scatter(vv[0][:, 0], vv[0][:, 1], vv[0][:, 2], color='r')
plt.show()



RE: How to draw rectangles in pyplot? - SuchtyTV - Jul-14-2019

Uff, my fault !!!

I confused triangle with a rectangle! I would like to draw triangles... Sorry, not a foreigner...