有没有可能使用‘`lego’绘图来可视化一个numpy数组?
例如:
numpy数组:
x = np.ones((3,3))应呈现为:

其中数组中的每个条目由一个框表示。
理想情况下,这应该适用于3d numpy数组
发布于 2020-09-12 00:00:26
这是可能的。这个例子主要是从astroML's page复制过来的。这不是动态的,但我希望它足以让您入门。
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8, 6), facecolor='w')
ax = plt.axes([0, 0, 1, 1], xticks=[], yticks=[], frameon=False)
def draw_cube(ax, xy, size, depth=0.4,
edges=None, label=None,
label_kwargs=None, **kwargs):
if edges is None:
edges = range(1, 13)
x, y = xy
if 1 in edges:
ax.plot([x, x + size],
[y + size, y + size], **kwargs)
if 2 in edges:
ax.plot([x + size, x + size],
[y, y + size], **kwargs)
if 3 in edges:
ax.plot([x, x + size],
[y, y], **kwargs)
if 4 in edges:
ax.plot([x, x],
[y, y + size], **kwargs)
if 5 in edges:
ax.plot([x, x + depth],
[y + size, y + depth + size], **kwargs)
if 6 in edges:
ax.plot([x + size, x + size + depth],
[y + size, y + depth + size], **kwargs)
if 7 in edges:
ax.plot([x + size, x + size + depth],
[y, y + depth], **kwargs)
if 8 in edges:
ax.plot([x, x + depth],
[y, y + depth], **kwargs)
if 9 in edges:
ax.plot([x + depth, x + depth + size],
[y + depth + size, y + depth + size], **kwargs)
if 10 in edges:
ax.plot([x + depth + size, x + depth + size],
[y + depth, y + depth + size], **kwargs)
if 11 in edges:
ax.plot([x + depth, x + depth + size],
[y + depth, y + depth], **kwargs)
if 12 in edges:
ax.plot([x + depth, x + depth],
[y + depth, y + depth + size], **kwargs)
if label:
if label_kwargs is None:
label_kwargs = {}
ax.text(x + 0.5 * size, y + 0.5 * size, label,
ha='center', va='center', **label_kwargs)
solid = dict(c='black', ls='-', lw=1,
label_kwargs=dict(color='k'))
depth=0.4
draw_cube(ax, (1, 2), 1, depth, [1, 2, 3, 4, 5, 6, 9], '1', **solid)
draw_cube(ax, (2, 2), 1, depth, [1, 2, 3, 6, 9], '1', **solid)
draw_cube(ax, (3, 2), 1, depth, [1, 2, 3, 6, 7, 9, 10], '1', **solid)
draw_cube(ax, (1, 1), 1, depth, [2, 3, 4], '1', **solid)
draw_cube(ax, (2, 1), 1, depth, [2, 3], '1', **solid)
draw_cube(ax, (3, 1), 1, depth, [2, 3, 7, 10], '1', **solid)
draw_cube(ax, (1, 0), 1, depth, [2, 3, 4], '1', **solid)
draw_cube(ax, (2, 0), 1, depth, [2, 3], '1', **solid)
draw_cube(ax, (3, 0), 1, depth, [2, 3, 7, 10], '1', **solid)
ax.set_xlim(0, 6)
ax.set_ylim(-1, 4)
plt.show()

https://stackoverflow.com/questions/63849347
复制相似问题