{"id":204642,"date":"2025-05-29T16:19:51","date_gmt":"2025-05-29T08:19:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204642\/"},"modified":"2025-05-29T16:19:51","modified_gmt":"2025-05-29T08:19:51","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-matplotlib-%e7%9a%84-3d-%e5%9b%be%e4%b8%ad%e7%bb%98%e5%88%b6%e7%9f%a2%e9%87%8f%e4%b8%8a%e7%9a%84%e7%ae%ad%e5%a4%b4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204642\/","title":{"rendered":"\u5982\u4f55\u5728 Matplotlib \u7684 3D \u56fe\u4e2d\u7ed8\u5236\u77e2\u91cf\u4e0a\u7684\u7bad\u5934\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Matplotlib \u7684 3D \u56fe\u4e2d\u7ed8\u5236\u77e2\u91cf\u4e0a\u7684\u7bad\u5934\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Matplotlib \u7684 3D \u56fe\u4e2d\u7ed8\u5236\u77e2\u91cf\u4e0a\u7684\u7bad\u5934\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313888966732e5e00c03a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 matplotlib \u7684 3d \u56fe\u4e2d\u7ed8\u5236\u77e2\u91cf\u4e0a\u7684\u7bad\u5934<\/strong><\/p>\n<p>\u60f3\u8981\u5c06\u7bad\u5934\u653e\u7f6e\u5728 3d \u56fe\u5f62\u4e2d\u7ed8\u5236\u7684\u7279\u5f81\u5411\u91cf\u4e0a\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u7bad\u5934\u8865\u4e01\u7c7b<\/strong><\/p>\n<p>\u7531\u4e8e matplotlib \u7684 fancyarrowpatch \u7c7b\u4ec5\u9002\u7528\u4e8e 2d \u56fe\u5f62\uff0c\u56e0\u6b64\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7bad\u5934\u8865\u4e01\u7c7b arrow3d\uff0c\u8be5\u7c7b\u7ee7\u627f\u81ea fancyarrowpatch\u3002\u8986\u76d6 posa \u548c posb \u65b9\u6cd5\uff0c\u5c06 3d \u5750\u6807\u6295\u5f71\u5230 2d\uff0c\u7136\u540e\u5c06\u5176\u5206\u914d\u7ed9 posa \u548c posb\u3002<\/p>\n<p><strong>\u4fee\u6539\u7ed8\u56fe\u4ee3\u7801<\/strong><\/p>\n<p>\u5728\u7ed8\u56fe\u4ee3\u7801\u4e2d\uff0c\u5c06\u7bad\u5934\u827a\u672f\u5bb6\u6dfb\u52a0\u5230\u5305\u542b\u7279\u5f81\u5411\u91cf\u7684\u7ed8\u56fe\u5bf9\u8c61\u4e2d\u3002\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 arrow3d \u7c7b\uff0c\u4e3a\u6bcf\u4e2a\u7279\u5f81\u5411\u91cf\u7ed8\u5236\u4e00\u4e2a\u7bad\u5934\uff0c\u4ece\u5e73\u5747\u4f4d\u7f6e\u5ef6\u4f38\u5230\u7279\u5f81\u5411\u91cf\u672b\u7aef\u3002<\/p>\n<p><strong>\u6700\u7ec8\u4ee3\u7801<\/strong><\/p>\n<pre>import numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib.patches import FancyArrowPatch\nfrom mpl_toolkits.mplot3d import proj3d\n\nclass Arrow3D(FancyArrowPatch):\n    def __init__(self, xs, ys, zs, *args, **kwargs):\n        FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)\n        self._verts3d = xs, ys, zs\n\n    def draw(self, renderer):\n        xs3d, ys3d, zs3d = self._verts3d\n        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)\n        self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))\n        FancyArrowPatch.draw(self, renderer)\n\n# \u6570\u636e\u751f\u6210\u548c\u7279\u5f81\u5411\u91cf\u8ba1\u7b97\n...\n\n# \u7ed8\u56fe\nfig = plt.figure(figsize=(15,15))\nax = fig.add_subplot(111, projection='3d')\n...\nfor v in eig_vec:\n    a = Arrow3D([mean_x, v[0]], [mean_y, v[1]],\n                [mean_z, v[2]], mutation_scale=20,\n                lw=3, arrowstyle=\"-|&gt;\", color=\"r\")\n    ax.add_artist(a)\n...<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<p>\u5c06\u770b\u5230\u5e26\u7bad\u5934\u7684\u7279\u5f81\u5411\u91cf\u7ed8\u5236\u5728 3d \u56fe\u4e2d\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5728 Matplotlib \u7684 3D \u56fe\u4e2d\u7ed8\u5236\u77e2\u91cf\u4e0a\u7684\u7bad\u5934\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Matplotlib \u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204642","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204642"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204642\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}