{"id":205091,"date":"2025-05-29T17:01:14","date_gmt":"2025-05-29T09:01:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205091\/"},"modified":"2025-05-29T17:01:14","modified_gmt":"2025-05-29T09:01:14","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-matplotlib-3d-%e5%9b%be%e4%b8%ad%e4%b8%ba%e7%9f%a2%e9%87%8f%e6%b7%bb%e5%8a%a0%e7%ae%ad%e5%a4%b4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205091\/","title":{"rendered":"\u5982\u4f55\u5728 Matplotlib 3D \u56fe\u4e2d\u4e3a\u77e2\u91cf\u6dfb\u52a0\u7bad\u5934\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Matplotlib 3D \u56fe\u4e2d\u4e3a\u77e2\u91cf\u6dfb\u52a0\u7bad\u5934\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323384816741633111387.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 matplotlib 3d \u56fe\u4e2d\u5c06\u7bad\u5934\u653e\u5728\u77e2\u91cf\u4e0a<\/strong><\/p>\n<p>\u5728\u7ed8\u5236 3d \u6570\u636e\u7684\u7279\u5f81\u5411\u91cf\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u8fd9\u4e9b\u5411\u91cf\u4e0a\u653e\u7f6e\u7bad\u5934\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a arrow3d \u7684\u81ea\u5b9a\u4e49\u7bad\u5934\u8865\u4e01\u7c7b\uff0c\u8be5\u7c7b\u6269\u5c55\u4e86 matplotlib \u7684 fancyarrowpatch\u3002<\/p>\n<p>arrow3d \u7c7b\u8986\u76d6\u4e86 fancyarrowpatch \u4e2d\u7684 posa \u548c posb \u5c5e\u6027\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5c5e\u6027\u6295\u5f71\u5230 2d \u4ee5\u5728 3d \u56fe\u4e2d\u7ed8\u5236\u7bad\u5934\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u4ece 3d \u5750\u6807\u521b\u5efa arrow3d \u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528 proj3d \u6a21\u5757\u5c06 3d \u5750\u6807\u6295\u5f71\u5230 2d\u3002<\/li>\n<li>\u5c06\u6295\u5f71\u7684 2d \u5750\u6807\u5206\u914d\u7ed9 posa \u548c posb \u5c5e\u6027\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u6211\u4eec\u4fee\u6539\u4e86\u539f\u59cb\u793a\u4f8b\u4ee3\u7801\uff0c\u6dfb\u52a0\u4e86 arrow3d \u7bad\u5934\u827a\u672f\u5bb6\uff1a<\/p>\n<pre>import numpy as np\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\nsamples, eig_vec = ...  # \u6837\u672c\u6570\u636e\u548c\u7279\u5f81\u5411\u91cf\u8ba1\u7b97\n\nfig = plt.figure(...)\nax = fig.add_subplot(111, projection='3d')\n\n...  # \u7ed8\u5236\u6563\u70b9\u548c\u7403\u5fc3\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)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<p>\u5728 3d \u6563\u70b9\u56fe\u4e0a\uff0c\u6bcf\u4e2a\u7279\u5f81\u5411\u91cf\u73b0\u5728\u90fd\u7ed8\u5236\u6709\u4e00\u4e2a\u6307\u5411\u5176\u65b9\u5411\u7684\u7bad\u5934\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Matplotlib 3&#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-205091","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205091","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=205091"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205091\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}