0

我试图在 3D 桁架示例上显示位移但是我遇到了一个错误。我在下面简化了我的代码。我能够在 2D 问题上显示位移但是我无法在 3D 问题上。我也在尝试显示每个节点的节点编号。我设法放置节点(绿色)但是即使在我使用“plt.annotate”命令后数字也没有显示。有人可以帮我显示位移和节点编号吗?谢谢你提前。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
np.set_printoptions(threshold=sys.maxsize)
def plot_truss(nodes, elements, areas,forces):
    # plot nodes in 3d 
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x = [i[0] for i in nodes.values()]
    y = [i[1] for i in nodes.values()]
    z = [i[2] for i in nodes.values()]
    # size = 400
    # ax.scatter(x, y, z, c='r', marker='o', s=size, zorder=5)
    size = 400
    offset = size / 4000
    ax.scatter(x, y, z, c='y', s=size, zorder=5)
    for i, location in enumerate(zip(x, y, z)):
        plt.annotate(i + 1, (location[0] - offset, location[1] - offset), zorder=10)
    # plot elements in 3d 
    for element in elements:
        fromPoint = np.array(nodes[elements[element][0]])
        toPoint = np.array(nodes[elements[element][1]])
        x1 = fromPoint[0]
        y1 = fromPoint[1]
        z1 = fromPoint[2]
        x2 = toPoint[0]
        y2 = toPoint[1]
        z2 = toPoint[2]
        ax.plot([x1, x2], [y1, y2], zs=[z1, z2], c='b', linestyle='-', linewidth=5*areas[element], zorder=1)
nodes = {1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}
areas = {1: 1.0, 2: 2.0, 3: 2.0}
elements = {1: [1, 3], 2: [2, 3], 3: [4, 3]}
forces = {1: [0, 0, 0], 2: [0, 0, 0], 3: [0, -200, 0], 4: [0, 0, 0]}
disps = {1: [0, 0, 0], 2: [0, 0, 0], 3: [ 3, -2,  4], 4: [0, 0, 0]}
def plt_displacement(nodes,elements,disps color="red"):
    nodes_disp = np.copy(nodes)
    nodes_disp[:, 0] += disp[::2, 0]
    nodes_disp[:, 1] += disp[1::2, 0]
    plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)
    for e in elements:
        x_tmp = [nodes_disp[e[0], 0], nodes_disp[e[1], 0]]
        y_tmp = [nodes_disp[e[0], 1], nodes_disp[e[1], 1]]
        plt.plot(x_tmp, y_tmp, color=color)
plt_displacement(nodes,elements,disps)       
plot_truss(nodes, elements, areas, forces)
plt.show()

当我运行代码时,出现以下错误;

<ipython-input-47-758895b259be> in plt_displacement(elements, nodes, disp, color)
     31 def plt_displacement(elements, nodes, disp, color="red"):
     32     nodes_disp = np.copy(nodes)
---> 33     nodes_disp[:, 0] += disp[::2, 0]
     34     nodes_disp[:, 1] += disp[1::2, 0]
     35     plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)

IndexError: too many indices for array
4

1 回答 1

0

看起来您可能在调用 plt_displacement() (第 3 行和第 12 行到最后一行)与您的定义之间切换了“节点”和“元素”。

plt_displacement(nodes,elements,disps)


def plt_displacement(elements, nodes, disp, color="red"):

我不确定 plt_displacement 到底应该做什么。但是看看 nodes_disp 它是一个没有形状的数组,所以切片是行不通的。

>>> nodes_disp = np.copy(nodes)
>>> nodes_disp
array({1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}, dtype=object)

>>> nodes_disp.shape
()

您可以将值更改为数组并将其切片,如下所示:

>>> npdisp = np.copy(list(disps.values()))

>>> nodes_disp
array([[ 0, 10,  0],
       [ 0,  0,  0],
       [10,  5,  0],
       [ 0, 10, 10]])

但我不确定这是否是你的意图。同样明智的是,您必须将 disp 的类型更改为数组才能对其进行切片,因为它是字典

于 2020-04-09T20:47:50.737 回答