【问题标题】:How can I display displacement and node numbers on a 3D truss?如何在 3D 桁架上显示位移和节点编号?
【发布时间】:2020-04-09 19:21:28
【问题描述】:

我试图在 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

【问题讨论】:

  • 看起来像一维数组的二维索引。你运行过调试器吗?

标签: python numpy matplotlib truss


【解决方案1】:

看起来您可能在调用 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 的类型更改为数组才能对其进行切片,因为它是一个字典

【讨论】:

  • 不幸的是,即使在正确切换后我仍然遇到同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
相关资源
最近更新 更多