使用matplotlib在单个轴上绘制大量轮廓时效率低下

0 投票
0 回答
21 浏览
提问于 2025-04-12 03:31

我的代码目的是从另一个软件提取的数据中绘制土壤压力图。这个软件的输出会给我每个壳体连接点的压力值,我需要对这些值进行插值,以便在整个壳体上展示压力。因为每个壳体的形状可能不同,而且一组壳体可能在几何形状上有凹陷和开口,所以我会在坐标轴上分别绘制每个壳体的应力。

问题是,随着壳体数量的增加,绘图的时间会呈指数级增长。比如说,绘制10张每张有100个壳体的图,比绘制一张有1000个壳体的图要快得多。举个例子,绘制一张有4000个壳体的图可能需要几十分钟,而我需要绘制好几张图,这样的执行时间对于日常使用来说实在太不方便了。

我想知道有没有办法加快这个绘图过程。如果我处理问题的方法不是最优的,我也欢迎其他建议。

下面是实现我上面描述功能的代码部分:

# Plotagem da tensão no solo no gráfico
        fig, ax = plt.subplots()
        levels = np.linspace(tensaoInf, tensaoSup, num=14).tolist()
        levels = [num * fator for num in levels]
        norm = mpl.colors.BoundaryNorm(levels, coresSAP.N, extend='both')

        print('  Desenhando a imagem...')
        comp = len(shellgroup.id_shells)
        i = 0

        for shell in shellgroup.id_shells:
            i += 1
            print(f'  Concluído {round(i * 100/comp, 1)}%', end='\r')
            plotx = []
            ploty = []
            plotz = []
            for joint in shellgroup.lista_joints[shell]:
                plotx.append(pontos_x[joint])
                ploty.append(pontos_y[joint])
                plotz.append(dicTensaoJoint[joint] * fator)

            xpt = np.array(plotx)
            ypt = np.array(ploty)
            zpt = np.array(plotz)
            linhas = ax.tricontour(xpt, ypt, zpt, levels=levels, colors='k', linewidths=esp_linha, linestyles='dashed')
            colorido = ax.tricontourf(xpt, ypt, zpt, levels=levels, norm=norm, cmap=coresSAP, extend='both')
            # ax.plot(xpt, ypt, 'ko', ms=2)

            pontos = np.array([(plotx[i], ploty[i]) for i in range(len(plotx))])
            hull = ConvexHull(pontos)
            for simplex in hull.simplices:
                ax.plot(pontos[simplex, 0], pontos[simplex, 1], 'k-', linewidth=esp_linha)

        # Desenho da escala de cores
        fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=coresSAP),
                     ax=ax, orientation='vertical', extendrect=True,
                     ticks=levels, extendfrac='auto', label=textoEscala,
                     fraction=fracao, pad=0.04, format="{x:.2f}")

        if not os.path.exists("./FigurasPySAP"):
            os.makedirs("./FigurasPySAP")
        fig.savefig(f"./FigurasPySAP/tensao-{grupo}-{combsolo}.png", dpi=400, bbox_inches='tight')

这是我生成的图像类型的一个例子

0 个回答

暂无回答

撰写回答