{"id":3177,"date":"2023-08-29T15:52:24","date_gmt":"2023-08-29T07:52:24","guid":{"rendered":"https:\/\/www.yatenglg.cn\/blog\/?p=3177"},"modified":"2023-08-29T15:52:47","modified_gmt":"2023-08-29T07:52:47","slug":"matplotlib-%e4%b8%89%e7%bb%b4%e7%bb%98%e5%88%b6%e6%95%a3%e7%82%b9%e4%b8%8e%e7%ba%bf%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=3177","title":{"rendered":"matplotlib \u4e09\u7ef4\u7ed8\u5236\u6563\u70b9\u4e0e\u7ebf\u6bb5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>from mpl_toolkits.mplot3d import Axes3D\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom laspy.file import File\n\n\ndef read_las_ins(las_file):\n    las = File(las_file, mode='r')\n    x = las.x\n    y = las.y\n    z = las.z\n    classification = las.classification  # \u6807\u7b7e,\u8fd9\u91cc\u6570\u636e\u7c7b\u522b\u662f\u5df2\u7ecf\u76f4\u63a5\u5b58\u5728las\u6587\u4ef6\u4e2d\u7684\n    red = np.unique(las.red, False, True)&#91;1]    # \u5047\u8bbe\u5b9e\u4f8b\u6807\u7b7e\u5199\u5728\u4e86R\u5b57\u6bb5\n    points = np.vstack((x, y, z, classification, red)).transpose()\n    return points\n\n\ndef plot_3d_with_line(xyz, classes=None, s=0.5, lines=None, cm='Set1'):\n    cm = plt.get_cmap(cm)\n    fig = plt.figure()\n    ax = fig.gca(projection='3d')\n    ax.set_xlabel(xlabel='x\/m',\n                  fontdict=None,\n                  labelpad=0,  # default: 4.0\n                  loc='right',  # default: 'center'\n                  )\n    ax.set_ylabel(ylabel='y\/m',\n                  fontdict=None,\n                  labelpad=0,  # default: 4.0\n                  # loc='left',  # default: 'center'\n                  )\n    ax.set_zlabel(zlabel='z\/m',\n                  fontdict=None,\n                  labelpad=0,  # default: 4.0\n                  # loc='left',  # default: 'center'\n                  )\n    if classes is not None:\n        ax.scatter(xyz&#91;:, 0], xyz&#91;:, 1], xyz&#91;:, 2], s=s, c=classes, cmap=cm)\n    else:\n        ax.scatter(xyz&#91;:, 0], xyz&#91;:, 1], xyz&#91;:, 2], s=s, cmap=cm)\n\n    if lines is not None:\n        for line in lines:\n            if line&#91;0]&#91;2] != line&#91;1]&#91;2]:\n                z = np.linspace(line&#91;0]&#91;2], line&#91;1]&#91;2])\n                x = (line&#91;1]&#91;0] - line&#91;0]&#91;0]) \/ (line&#91;1]&#91;2] - line&#91;0]&#91;2]) * z + \\\n                    line&#91;1]&#91;0] - (line&#91;1]&#91;0] - line&#91;0]&#91;0]) \/ (line&#91;1]&#91;2] - line&#91;0]&#91;2]) * line&#91;0]&#91;0]\n                y = (line&#91;1]&#91;1] - line&#91;0]&#91;1]) \/ (line&#91;1]&#91;2] - line&#91;0]&#91;2]) * z + \\\n                    line&#91;1]&#91;1] - (line&#91;1]&#91;1] - line&#91;0]&#91;1]) \/ (line&#91;1]&#91;2] - line&#91;0]&#91;2]) * line&#91;0]&#91;1]\n                ax.plot3D(x, y, z, 'green')\n\n            elif line&#91;0]&#91;1] != line&#91;1]&#91;1]:\n                y = np.linspace(line&#91;0]&#91;1], line&#91;1]&#91;1])\n                x = (line&#91;1]&#91;0] - line&#91;0]&#91;0]) \/ (line&#91;1]&#91;1] - line&#91;0]&#91;1]) * y + \\\n                    line&#91;1]&#91;0] - (line&#91;1]&#91;0] - line&#91;0]&#91;0]) \/ (line&#91;1]&#91;1] - line&#91;0]&#91;1]) * line&#91;0]&#91;0]\n                z = (line&#91;1]&#91;2] - line&#91;0]&#91;2]) \/ (line&#91;1]&#91;1] - line&#91;0]&#91;1]) * y + \\\n                    line&#91;1]&#91;2] - (line&#91;1]&#91;2] - line&#91;0]&#91;2]) \/ (line&#91;1]&#91;1] - line&#91;0]&#91;1]) * line&#91;0]&#91;2]\n                ax.plot3D(x, y, z, 'green')\n            else:\n                x = np.linspace(line&#91;0]&#91;0], line&#91;1]&#91;0])\n                y = (line&#91;1]&#91;1] - line&#91;0]&#91;1]) \/ (line&#91;1]&#91;0] - line&#91;0]&#91;0]) * x + \\\n                    line&#91;1]&#91;1] - (line&#91;1]&#91;1] - line&#91;0]&#91;1]) \/ (line&#91;1]&#91;0] - line&#91;0]&#91;0]) * line&#91;0]&#91;1]\n                z = (line&#91;1]&#91;2] - line&#91;0]&#91;2]) \/ (line&#91;1]&#91;0] - line&#91;0]&#91;0]) * x + \\\n                    line&#91;1]&#91;2] - (line&#91;1]&#91;2] - line&#91;0]&#91;2]) \/ (line&#91;1]&#91;0] - line&#91;0]&#91;0]) * line&#91;0]&#91;2]\n                ax.plot3D(x, y, z, 'green')\n    plt.show()\n    return True\n\n\nlas_file = \"xxx.las\"\n\npoints = read_las_ins(las_file)\n\nxyz = points&#91;:, : 3]\nclasses = points&#91;:, 3]\ninstance = points&#91;:, 4]\n\nlines = &#91;((0,0,0), (100, 50, 100))]\nplot_3d_with_line(xyz=xyz, classes=instance, lines=lines, cm='tab20')\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3177"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3177"}],"version-history":[{"count":3,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3177\/revisions"}],"predecessor-version":[{"id":3180,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3177\/revisions\/3180"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}