首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用3d- lego图显示numpy数组

使用3d- lego图显示numpy数组
EN

Stack Overflow用户
提问于 2020-09-11 22:45:07
回答 1查看 56关注 0票数 1

有没有可能使用‘`lego’绘图来可视化一个numpy数组?

例如:

numpy数组:

代码语言:javascript
复制
x = np.ones((3,3))

应呈现为:

其中数组中的每个条目由一个框表示。

理想情况下,这应该适用于3d numpy数组

EN

回答 1

Stack Overflow用户

发布于 2020-09-12 00:00:26

这是可能的。这个例子主要是从astroML's page复制过来的。这不是动态的,但我希望它足以让您入门。

代码语言:javascript
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63849347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档