我想创建桁架的图形。我有两个清单;给出了条形编号以及构成它们的节点,它看起来像这样:
\nelements = [[1, 1, 2], [2, 2, 3], [3, 3, 4], [4, 4, 5], [5, 5, 1], [6, 1, 4], [7, 2, 4], [8, 4, 6], [9, 6, 5]]\nRun Code Online (Sandbox Code Playgroud)\n例如,元素一由节点 1 和节点 2 组成。另一个数据集给出节点及其在 2D 平面中的坐标。它看起来像这样:
\nnodes= [[1.0, 0.0, 1.2], [2.0, -1.5, 1.2], [3.0, -1.5, 0.0], [4.0, 0.0, 0.0], [5.0, 1.5, 1.2], [6.0, 1.5, 0.0]]\nRun Code Online (Sandbox Code Playgroud)\n例如,节点 1 的坐标为 (0.0, 1.2)。
\n我想使用上面的列表通过 pyplot 重新创建以下图形。其中 A 为 1.5m,B 为 1.2m。
\n\n我想做这样的事情:
\ndef draw(nodes):\n draw1=[]\n for i in range(len(nodes)):\n for j in range(len(nodes[1])):\n mmc+=[(nodes[i][j][1][1],nodes[i][j][1][2])]\n for t in [draw1]:\n xs1, ys1 = zip(*t)\n plot(xs1, ys1, \'o\')\n plot(xs1, ys1, \'-\')\n plot.show()\nRun Code Online (Sandbox Code Playgroud)\n我知道我没有做正确的事情(超出索引),但我不\xc2\xb4t 真的知道如何解决它。
\n你的问题太宽泛了,我只提供第一部分的答案,给定节点坐标列表和连接列表,如何绘制桁架?
首先,如果您正在实现一个用于桁架分析的程序,您可能会使用一个数据类并存储有关每个钢筋的更多信息,但作为初学者,我们只需要连接性,我写道:
In [8]: class Bar():
...: def __init__(self, i, j):
...: self.i = i
...: self.j = j
Run Code Online (Sandbox Code Playgroud)
接下来是节点坐标列表和条形列表
In [9]: node_xy = [[0,0], [1,2], [2,0], [3,2], [4,0]]
...:
...: bars = [Bar(0,1), Bar(1,2), Bar(2,3), Bar(3,4),
...: Bar(0,2), Bar(2,4),
...: Bar(1,3)]
Run Code Online (Sandbox Code Playgroud)
绘图,抽象为函数
In [10]: def graph(node_xy, bars, figsize=(8,8)):
...: from matplotlib.ticker import MultipleLocator as ml
...: from matplotlib.pyplot import annotate, subplots, show
...: fig, ax = subplots(figsize=figsize, layout='constrained')
...: ax.scatter(*zip(*node_xy), s=90, zorder=8, color='w', edgecolor='k')
...: for bar in bars:
...: xi, yi = node_xy[bar.i]
...: xj, yj = node_xy[bar.j]
...: ax.plot((xi, xj), (yi, yj), color='b', lw=2.5, zorder=6)
...: for s in ax.spines.values() : s.set_visible(0)
...: ax.xaxis.set_major_locator(ml(xmajor))
...: ax.xaxis.set_minor_locator(ml(xminor))
...: ax.yaxis.set_major_locator(ml(ymajor))
...: ax.yaxis.set_minor_locator(ml(yminor))
...: ax.tick_params(length=0, axis='both', which='both')
...: ax.grid(1)
...: ax.grid(1, 'minor', color='#ddeeff')
...: ax.set_aspect(1)
...: show()
...: return fig, ax
Run Code Online (Sandbox Code Playgroud)
最后,我们执行我们的函数
In [11]: xmajor, xminor = 2, .5
...: ymajor, yminor = 2, .5
...: fig, ax = graph(node_xy, bars, figsize=(8,4))
Run Code Online (Sandbox Code Playgroud)
以上对您有帮助吗?
因为我不会扩展我的答案,所以如果您无法从这个起点开始,那么您应该提出进一步的、更有针对性的问题。