I have a function plotting the cylinder, using matplotlib.
我想知道如何使用情节来做同样的事情?
下面是我绘制圆柱体的 matplotlib 函数:
#function to plot the cylinder
def _plotCylinder(self, ax, x, y, z, dx, dy, dz, color='red',mode=2):
""" Auxiliary function to plot a Cylinder """
# plot the two circles above and below the cylinder
p = Circle((x+dx/2,y+dy/2),radius=dx/2,color=color,ec='black')
p2 = Circle((x+dx/2,y+dy/2),radius=dx/2,color=color,ec='black')
ax.add_patch(p)
ax.add_patch(p2)
art3d.pathpatch_2d_to_3d(p, z=z, zdir="z")
art3d.pathpatch_2d_to_3d(p2, z=z+dz, zdir="z")
# plot a circle in the middle of the cylinder
center_z = np.linspace(0, dz, 15)
theta = np.linspace(0, 2*np.pi, 15)
theta_grid, z_grid=np.meshgrid(theta, center_z)
x_grid = dx / 2 * np.cos(theta_grid) + x + dx / 2
y_grid = dy / 2 * np.sin(theta_grid) + y + dy / 2
z_grid = z_grid + z
ax.plot_surface(x_grid, y_grid, z_grid,shade=False,fc=color,ec='black',alpha=1,color=color)
x,y,z = item.position
[w,h,d] = item.getDimension()
# plot item of cylinder
self._plotCylinder(axGlob, float(x), float(y), float(z), float(w),float(h),float(d),color=color,mode=2)
更新 - 用于绘制立方体类型项目的当前代码
### PLOTLY ###
# https://plotly.com/python/3d-mesh/#mesh-cube
def vertices(xmin=0, ymin=0, zmin=0, xmax=1, ymax=1, zmax=1):
return {
"x": [xmin, xmin, xmax, xmax, xmin, xmin, xmax, xmax],
"y": [ymin, ymax, ymax, ymin, ymin, ymax, ymax, ymin],
"z": [zmin, zmin, zmin, zmin, zmax, zmax, zmax, zmax],
"i": [7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6],
"j": [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3],
"k": [0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2],
}
def parallelipipedic_frame(xm, xM, ym, yM, zm, zM):
# defines the coords of each segment followed by None, if the line is
# discontinuous
x = [xm, xM, xM, xm, xm, None, xm, xM, xM, xm, xm, None, xm, xm, None, xM, xM,
None, xM, xM, None, xm, xm]
y = [ym, ym, yM, yM, ym, None, ym, ym, yM, yM, ym, None, ym, ym, None, ym, ym,
None, yM, yM, None, yM, yM]
z = [zm, zm, zm, zm, zm, None, zM, zM, zM, zM, zM, None, zm, zM, None, zm, zM,
None, zm, zM, None, zm, zM]
return x, y, z
# take a packer item and build parameters to a plotly mesh3d cube
def packer_to_plotly(item):
colors = ["crimson", "limegreen", "green", "red", "cyan", "magenta", "yellow"]
ret = vertices(
*item.position, *[sum(x) for x in zip(item.position, item.getDimension())]
)
ret["name"] = item.name
ret["color"] = colors[ord(item.name.split("_")[0][-1]) - ord("A")]
return ret
# create a multi-plot figure for each bin
# fig = make_subplots(rows=len(packer.bins), cols=1, specs=[[{"type":"mesh3d"}], [{"type":"mesh3d"}]])
fig = go.Figure()
# add a trace for each packer item
for row, pbin in enumerate(packer.bins):
for item in pbin.items:
fig.add_trace(go.Mesh3d(packer_to_plotly(item)))
# sorting out layout, prmarily aspect ratio
fig.update_layout(
margin={"l": 0, "r": 0, "t": 0, "b": 0},
autosize=False,
scene=dict(
camera=dict(
# eye=dict(x=0.1, y=0.1, z=1.5)
),
aspectratio=dict(x=1, y=.2, z=0.2),
aspectmode="manual",
),
)
# push data into a data frame to enable more types of analysis
df = pd.DataFrame(
[
{
"bin_name": b.partno,
"bin_index": i,
**packer_to_plotly(item),
**{d: v for v, d in zip(item.getDimension(), list("hwl"))},
**{d + d: v for v, d in zip(item.position, list("xyz"))},
}
for i, b in enumerate(packer.bins)
for item in b.items
]
)
# create a figure for each container (bin)
for pbin, d in df.groupby("bin_name"):
fig = go.Figure()
xx = []
yy = []
zz = []
# create a trace for each box (bin)
for _, r in d.iterrows():
fig.add_trace(
go.Mesh3d(r[["x", "y", "z", "i", "j", "k", "name", "color"]].to_dict())
)
xx += [r.xx, r.xx + r.h, r.xx + r.h, r.xx, r.xx, None] * 2 + [r.xx] * 5 + [None]
yy += [r.yy, r.yy, r.yy + r.w, r.yy + r.w, r.yy, None] * 2 + [
r.yy,
r.yy + r.w,
r.yy + r.w,
r.yy,
r.yy,
None,
]
zz += (
[r.zz] * 5
+ [None]
+ [r.zz + r.l] * 5
+ [None]
+ [r.zz, r.zz, r.zz + r.l, r.zz + r.l, r.zz, None]
)
fig.add_trace(
go.Scatter3d(
x=xx,
y=yy,
z=zz,
mode="lines",
line_color="black",
line_width=2,
hoverinfo="skip",
)
)
x, y, z = parallelipipedic_frame(0, 1202.4, 0, 235, 0, 269.7)
fig.add_trace(
go.Scatter3d(
x=x,
y=y,
z=z,
mode="lines",
line_color="blue",
line_width=2,
hoverinfo="skip",
)
)
ar = 4
xr = max(d["x"].max()) - min(d["x"].min())
fig.update_layout(
showlegend=False,
title={"text": pbin, "y": 0.9, "x": 0.5, "xanchor": "center", "yanchor": "top"},
margin={"l": 0, "r": 0, "t": 0, "b": 0},
# autosize=False,
scene=dict(
camera=dict(eye=dict(x=2, y=2, z=2)),
aspectratio={
**{"x": ar},
**{
c: ((max(d[c].max()) - min(d[c].min())) / xr) * ar
for c in list("yz")
},
},
aspectmode="manual",
),
)
fig.show()