Brittney P.

# Plotting a 3D rectangle, with vertex starting at the origin, from a list of dimensions.

Hi,

I have been struggling to find a resource that I truly understand and that is exactly what I'm looking for. I'm currently learning python, but working on it for my senior research project.

So, I have created a list that has randomized dimensions within in it. I want to take the dimensions and plot a series of 3D rectangles that appear one after another in a row and also can stack up on one another (eventually until each column hits a certain volume). I haven't had any luck plotting 3D rectangles. Does anyone know a way I can do this based on the dimensions within the list?

Here is my code:

Also, I want to note that this specifically does a birdseye view.

import numpy

from matplotlib.collections import PatchCollection

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.patches as patches

#%matplotlib inline

import random

def createBox():

#this is what randomly generates the dimensions of the boxes and saves to list

N= int(input ("Enter number of boxes :"))

list1 = [];

list2 = [];

list3 = [];

list4 = [];

list5 = [];

for i in range(0,N):

x = random.randint(1,4) #non integers would be random.random or random.unif

y = random.randint(1,4)

z = random.randint(1,4)

box = [x,y,z];

area = [x,y]

list1.append(box)

list2.append(area)

print(list1)

print(list2)

#Here is where the organizes the dimensions of the box

for i in list2:

result = numpy.prod(i)

list3.append(result)

list3.sort(reverse = True)

print(list3)

for box in list2:

l = box[0] #this is along the x-axis

w = box[1] #this is along the y-axis

if l < w: #rotates the box 90 degrees, where largest size is along x-axis

l = box[1]

w = box[0]

res = [l,w]

list4.append(res)

list4.sort(reverse = True)

print(list4)

lims = (0,15)

fig1 = plt.figure()

ax1 = fig1.add_subplot(111, aspect = 'equal')

plt.ylim(lims)

plt.xlim(lims)

patchlist = [];

x = 0

y = 0

for box in list4:

l1 = box[0] #along the x-axis

w1 = box[1] #along the y-axis

list5.append(w1)

if sum(list5) <= 12:

rec = patches.Rectangle((x,y),l1,w1,fill = True, edgecolor= 'black', linewidth = 2.0 , linestyle='-')

# patchlist.append(patches.Rectangle((x,y),l1,w1,fill = False, edgecolor= 'black', linewidth = 2.0 , linestyle='-'))

y = y + w1

else:

x = x + 4

y = 0

list5 = [w1]

rec = patches.Rectangle((x,y),l1,w1,fill = True, edgecolor ='black',linewidth = 2.0, linestyle='-')

# patchlist.append(patches.Rectangle((x + l1,y),l1,w1,fill = False, edgecolor ='black',linewidth = 2.0, linestyle='-'))

y = y + w1

plt.show()

By:

Tutor
New to Wyzant

Independent Contractor - Python Specialty

Brittney P.

Thank you so much!!
Report

04/08/21

## Still looking for help? Get the right answer, fast.

Get a free answer to a quick problem.
Most questions answered within 4 hours.

#### OR

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.