| 1 |
import numpy as np |
| 2 |
import matplotlib.pyplot as plt |
| 3 |
import matplotlib.table |
| 4 |
from mpl_toolkits.mplot3d import Axes3D |
| 5 |
from mpl_toolkits.mplot3d.art3d import Poly3DCollection |
| 6 |
import matplotlib.patches as patches |
| 7 |
|
| 8 |
import docutils |
| 9 |
|
| 10 |
def plotcubus(axis, pos, ext, color): |
| 11 |
xs = pos[0] |
| 12 |
ys = pos[1] |
| 13 |
zs = pos[2] |
| 14 |
xe = xs + ext[0] |
| 15 |
ye = xs + ext[1] |
| 16 |
ze = zs + ext[2] |
| 17 |
|
| 18 |
|
| 19 |
x = [xs, xs, xe, xe, xs] |
| 20 |
y = [ys, ys, ys, ys, ys] |
| 21 |
z = [zs, ze, ze, zs, zs] |
| 22 |
verts = [list(zip(x, y, z))] |
| 23 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, linestyle='--', edgecolors='gray') |
| 24 |
poly.set_facecolor(color) |
| 25 |
axis.add_collection3d(poly) |
| 26 |
|
| 27 |
|
| 28 |
x = [xs, xe, xe, xs, xs] |
| 29 |
y = [ys, ys, ye, ye, ys] |
| 30 |
z = [zs, zs, zs, zs, zs] |
| 31 |
verts = [list(zip(x, y, z))] |
| 32 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, linestyle='--', edgecolors='gray') |
| 33 |
poly.set_facecolor(color) |
| 34 |
axis.add_collection3d(poly) |
| 35 |
|
| 36 |
|
| 37 |
x = [xs, xe, xe, xs, xs] |
| 38 |
y = [ys, ys, ye, ye, ys] |
| 39 |
z = [ze, ze, ze, ze, ze] |
| 40 |
verts = [list(zip(x, y, z))] |
| 41 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, linestyle='--', edgecolors='gray') |
| 42 |
poly.set_facecolor(color) |
| 43 |
axis.add_collection3d(poly) |
| 44 |
|
| 45 |
|
| 46 |
x = [xs, xs, xe, xe, xs] |
| 47 |
y = [ye, ye, ye, ye, ye] |
| 48 |
z = [zs, ze, ze, zs, zs] |
| 49 |
verts = [list(zip(x, y, z))] |
| 50 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, linestyle='--', edgecolors='gray') |
| 51 |
poly.set_facecolor(color) |
| 52 |
axis.add_collection3d(poly) |
| 53 |
|
| 54 |
|
| 55 |
x = [xs, xs, xs, xs, xs] |
| 56 |
y = [ys, ye, ye, ys, ys] |
| 57 |
z = [zs, zs, ze, ze, zs] |
| 58 |
verts = [list(zip(x, y, z))] |
| 59 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, edgecolors='gray', linestyle='--') |
| 60 |
poly.set_facecolor(color) |
| 61 |
axis.add_collection3d(poly) |
| 62 |
|
| 63 |
|
| 64 |
x = [xe, xe, xe, xe, xe] |
| 65 |
y = [ys, ye, ye, ys, ys] |
| 66 |
z = [zs, zs, ze, ze, zs] |
| 67 |
verts = [list(zip(x, y, z))] |
| 68 |
poly = Poly3DCollection(verts, alpha=0.1, lw=0.75, edgecolors='gray', linestyle='--') |
| 69 |
poly.set_facecolor(color) |
| 70 |
axis.add_collection3d(poly) |
| 71 |
axis.scatter(xs, ys, zs, marker='.', s=50, facecolor='r', edgecolor='k') |
| 72 |
axis.plot([xs, xe], [ys, ys], [zs, zs], lw=1.5, color='r') |
| 73 |
axis.plot([xs, xs], [ys, ye], [zs, zs], lw=1.5, color='r') |
| 74 |
axis.plot([xs, xs], [ys, ys], [zs, ze], lw=1.5, color='r') |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
def plot_2d(): |
| 79 |
positions = [[10, 90], [60, 5]] |
| 80 |
extents = [[30, -30], [30, 20]] |
| 81 |
colors = ['dodgerblue', 'crimson'] |
| 82 |
|
| 83 |
fig = plt.figure() |
| 84 |
fig.set_size_inches(5.5, 2.5) |
| 85 |
ax = fig.add_subplot(121) |
| 86 |
ax.set_xlim([0, 100]) |
| 87 |
ax.set_xticks([0, 25, 50, 75, 100]) |
| 88 |
ax.set_xlabel('width [mm]') |
| 89 |
ax.set_ylim([0, 100]) |
| 90 |
ax.set_ylabel('height [mm]') |
| 91 |
ax.set_yticks([0, 25, 50, 75, 100]) |
| 92 |
rect = patches.Rectangle((10, 90), 30, -30, alpha=0.25, facecolor="dodgerblue", edgecolor='k', |
| 93 |
lw=0.75, ls='--') |
| 94 |
ax.add_patch(rect) |
| 95 |
rect = patches.Rectangle((60, 5), 30, 20, alpha=0.25, facecolor="crimson", edgecolor='k', |
| 96 |
lw=0.75, ls='--') |
| 97 |
ax.add_patch(rect) |
| 98 |
ax.scatter([10, 60], [90, 5], marker=".", s=50, lw=0.25, facecolor="r", edgecolor='k') |
| 99 |
ax.plot([10, 40], [90, 90], lw=1.5, color='r') |
| 100 |
ax.plot([10, 10], [90, 60], lw=1.5, color='r') |
| 101 |
ax.plot([60, 60], [5, 25], lw=1.5, color='r') |
| 102 |
ax.plot([60, 90], [5, 5], lw=1.5, color='r') |
| 103 |
|
| 104 |
col_labels = ["Pos 1", "Pos 2"] |
| 105 |
rows_2d = ["width", "height"] |
| 106 |
cell_text = [[positions[0][0], positions[1][0]], |
| 107 |
[positions[0][1], positions[1][1]]] |
| 108 |
|
| 109 |
table = matplotlib.table.table(ax, cellText=cell_text, rowLabels=rows_2d, |
| 110 |
colLabels=col_labels, |
| 111 |
cellLoc="center", bbox=[1.6, 0.6, 0.4, 0.3], |
| 112 |
colWidths=[0.2 for c in col_labels], |
| 113 |
colColours=colors) |
| 114 |
ax.text(130, 100, "Positions DataArray") |
| 115 |
table.auto_set_font_size(False) |
| 116 |
table.set_fontsize(8) |
| 117 |
|
| 118 |
col_labels = ["Ext 1", "Ext 2"] |
| 119 |
cell_text = [[extents[0][0], extents[1][0]], |
| 120 |
[extents[0][1], extents[1][1]]] |
| 121 |
|
| 122 |
table = matplotlib.table.table(ax, cellText=cell_text, rowLabels=rows_2d, |
| 123 |
colLabels=col_labels, |
| 124 |
cellLoc="center", bbox=[1.6, 0.1, 0.4, 0.3], |
| 125 |
colWidths=[0.2 for c in col_labels], |
| 126 |
colColours=colors) |
| 127 |
ax.text(130, 100, "Positions DataArray") |
| 128 |
table.auto_set_font_size(False) |
| 129 |
table.set_fontsize(8) |
| 130 |
ax.text(130, 45, "Extents DataArray") |
| 131 |
|
| 132 |
ax.spines["top"].set_visible(False) |
| 133 |
ax.spines['right'].set_visible(False) |
| 134 |
fig.subplots_adjust(bottom=0.175, left=0.15, right=0.95, top=0.95) |
| 135 |
if docutils.is_running_under_pytest(): |
| 136 |
plt.close() |
| 137 |
else: |
| 138 |
fig.savefig("../images/2d_mtag.png") |
| 139 |
plt.show() |
| 140 |
|
| 141 |
|
| 142 |
def plot_3d(): |
| 143 |
positions = [[10, 10, 90], [60, 5, 20]] |
| 144 |
extents = [[30, 40, -30], [30, 60, 20]] |
| 145 |
colors = ['dodgerblue', 'crimson'] |
| 146 |
fig = plt.figure() |
| 147 |
fig.set_size_inches(5.5, 2.5) |
| 148 |
ax = plt.subplot2grid((2, 5), (0, 0), colspan=3, rowspan=2, projection='3d') |
| 149 |
|
| 150 |
ax.set_xlim([0, 100]) |
| 151 |
ax.set_xticks([0, 25, 50, 75, 100]) |
| 152 |
ax.set_xlabel('width [mm]') |
| 153 |
ax.set_ylim([0, 100]) |
| 154 |
ax.set_yticks([0, 25, 50, 75, 100]) |
| 155 |
|
| 156 |
ax.set_ylabel('depth [mm]') |
| 157 |
ax.set_zlabel('height [mm]') |
| 158 |
ax.set_zlim([0, 100]) |
| 159 |
ax.set_zticks([0, 25, 50, 75, 100]) |
| 160 |
|
| 161 |
for i in range(len(positions)): |
| 162 |
plotcubus(ax, positions[i], extents[i], colors[i]) |
| 163 |
|
| 164 |
col_labels = ["Pos 1", "Pos 2"] |
| 165 |
rows_3d = ["width", "height", "depth"] |
| 166 |
|
| 167 |
cell_text = [[positions[0][0], positions[1][0]], [positions[0][2], positions[1][2]], |
| 168 |
[positions[0][1], positions[1][1]]] |
| 169 |
|
| 170 |
table = matplotlib.table.table(ax, cellText=cell_text, rowLabels=rows_3d, colLabels=col_labels, |
| 171 |
cellLoc="center", bbox=[1.4, 0.5, 0.25, 0.3], |
| 172 |
colWidths=[0.1 for c in col_labels], |
| 173 |
colColours=colors) |
| 174 |
ax.text(170, 100, 130.0, "Positions DataArray") |
| 175 |
table.auto_set_font_size(False) |
| 176 |
table.set_fontsize(8) |
| 177 |
|
| 178 |
col_labels = ["Ext 1", "Ext 2"] |
| 179 |
cell_text = [[extents[0][0], extents[1][0]], [extents[0][2], extents[1][2]], |
| 180 |
[extents[0][1], extents[1][1]]] |
| 181 |
table = matplotlib.table.table(ax, cellText=cell_text, rowLabels=rows_3d, colLabels=col_labels, |
| 182 |
cellLoc="center", bbox=[1.4, -.1, 0.25, 0.3], |
| 183 |
colWidths=[0.1 for c in col_labels], |
| 184 |
colColours=colors) |
| 185 |
table.auto_set_font_size(False) |
| 186 |
table.set_fontsize(8) |
| 187 |
ax.text(180, 100, 5.0, "Extents DataArray") |
| 188 |
fig.subplots_adjust(bottom=0.1, left=0., right=0.95, top=0.95) |
| 189 |
|
| 190 |
if docutils.is_running_under_pytest(): |
| 191 |
plt.close() |
| 192 |
else: |
| 193 |
fig.savefig("../images/3d_mtag.png") |
| 194 |
plt.show() |
| 195 |
|
| 196 |
|
| 197 |
if __name__ == "__main__": |
| 198 |
plot_2d() |
| 199 |
plot_3d() |