{"id":1082446,"date":"2025-01-08T12:48:53","date_gmt":"2025-01-08T04:48:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082446.html"},"modified":"2025-01-08T12:48:55","modified_gmt":"2025-01-08T04:48:55","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%9d%90%e6%a0%87%e8%bd%b4%e7%ae%ad%e5%a4%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082446.html","title":{"rendered":"\u5982\u4f55\u7528Python\u753b\u5750\u6807\u8f74\u7bad\u5934"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183947\/96b3572e-37df-4e3f-be20-ffdfc2706d0d.webp\" alt=\"\u5982\u4f55\u7528Python\u753b\u5750\u6807\u8f74\u7bad\u5934\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u753b\u5750\u6807\u8f74\u7bad\u5934\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\uff0c\u8bf8\u5982Matplotlib\u3001Plotly\u548cSeaborn\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e13\u4e1a\u7684\u56fe\u8868\uff0c\u5e76\u6dfb\u52a0\u5750\u6807\u8f74\u7bad\u5934\u6765\u589e\u5f3a\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u7ed8\u5236\u5e26\u6709\u7bad\u5934\u7684\u5750\u6807\u8f74\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u8fd9\u4e2a\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u57fa\u672c\u5750\u6807\u8f74<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u7ed8\u5236\u57fa\u672c\u5750\u6807\u8f74\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.figure()<\/p>\n<p>plt.plot([0, 1], [0, 1])<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Basic Coordinate Axes&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u7bad\u5934\u5230\u5750\u6807\u8f74<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u5750\u6807\u8f74\u7684\u672b\u7aef\u6dfb\u52a0\u7bad\u5934\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u56fe\u8868\u7684\u4efb\u4f55\u4f4d\u7f6e\u6dfb\u52a0\u6ce8\u91ca\uff0c\u5f53\u7136\u4e5f\u5305\u62ec\u7bad\u5934\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728\u5750\u6807\u8f74\u7684\u672b\u7aef\u6dfb\u52a0\u7bad\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([0, 1], [0, 1])<\/p>\n<h2><strong>\u6dfb\u52a0X\u8f74\u7bad\u5934<\/strong><\/h2>\n<p>ax.annotate(&#39;&#39;, xy=(1, 0), xytext=(0, 0),<\/p>\n<p>            arrowprops=dict(arrowstyle=&quot;-&gt;&quot;))<\/p>\n<h2><strong>\u6dfb\u52a0Y\u8f74\u7bad\u5934<\/strong><\/h2>\n<p>ax.annotate(&#39;&#39;, xy=(0, 1), xytext=(0, 0),<\/p>\n<p>            arrowprops=dict(arrowstyle=&quot;-&gt;&quot;))<\/p>\n<p>ax.set_xlim(-0.1, 1.1)<\/p>\n<p>ax.set_ylim(-0.1, 1.1)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax.set_title(&#39;Coordinate Axes with Arrows&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0\u4e00\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Matplotlib\u7684<code>annotate<\/code>\u51fd\u6570\u6765\u5728\u5750\u6807\u8f74\u7684\u672b\u7aef\u6dfb\u52a0\u7bad\u5934\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e<code>annotate<\/code>\u51fd\u6570\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6dfb\u52a0\u6587\u672c\u6ce8\u91ca\uff0c\u8fd8\u53ef\u4ee5\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u7bad\u5934\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1. <code>annotate<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>annotate<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.annotate(text, xy, xytext, arrowprops)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>text<\/strong>\uff1a\u8981\u6ce8\u91ca\u7684\u6587\u672c\u3002\u53ef\u4ee5\u4e3a\u7a7a\u5b57\u7b26\u4e32\u4ee5\u53ea\u6dfb\u52a0\u7bad\u5934\u3002<\/li>\n<li><strong>xy<\/strong>\uff1a\u7bad\u5934\u6307\u5411\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>xytext<\/strong>\uff1a\u6ce8\u91ca\u6587\u672c\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>arrowprops<\/strong>\uff1a\u7bad\u5934\u7684\u5c5e\u6027\u5b57\u5178\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u7bad\u5934\u7684\u6837\u5f0f\u3001\u989c\u8272\u7b49\u3002<\/li>\n<\/ul>\n<p><h4>2. <code>arrowprops<\/code>\u53c2\u6570\u8be6\u89e3<\/h4>\n<\/p>\n<p><p><code>arrowprops<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u7bad\u5934\u7684\u5404\u79cd\u5c5e\u6027\u3002\u5e38\u7528\u7684\u5c5e\u6027\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>arrowstyle<\/strong>\uff1a\u7bad\u5934\u7684\u6837\u5f0f\uff0c\u53ef\u4ee5\u662f<code>-&gt;<\/code>\u3001<code>-|&gt;<\/code>\u7b49\u3002<\/li>\n<li><strong>color<\/strong>\uff1a\u7bad\u5934\u7684\u989c\u8272\u3002<\/li>\n<li><strong>linewidth<\/strong>\uff1a\u7bad\u5934\u7684\u7ebf\u5bbd\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, color=&#39;red&#39;, linewidth=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u5750\u6807\u8f74\u7bad\u5934\u7684\u7ed8\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528FancyArrowPatch<\/h4>\n<\/p>\n<p><p><code>FancyArrowPatch<\/code>\u662fMatplotlib\u4e2d\u4e00\u4e2a\u66f4\u5f3a\u5927\u7684\u7ed8\u5236\u7bad\u5934\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u7075\u6d3b\u548c\u590d\u6742\u7684\u7bad\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.patches import FancyArrowPatch<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([0, 1], [0, 1])<\/p>\n<p>arrow = FancyArrowPatch((0, 0), (1, 0), mutation_scale=20, color=&#39;blue&#39;)<\/p>\n<p>ax.add_patch(arrow)<\/p>\n<p>arrow = FancyArrowPatch((0, 0), (0, 1), mutation_scale=20, color=&#39;blue&#39;)<\/p>\n<p>ax.add_patch(arrow)<\/p>\n<p>ax.set_xlim(-0.1, 1.1)<\/p>\n<p>ax.set_ylim(-0.1, 1.1)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax.set_title(&#39;Coordinate Axes with Fancy ArrowPatch&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u7bad\u5934\u51fd\u6570<\/h4>\n<\/p>\n<p><p>Matplotlib\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684<code>arrow<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u7ed8\u5236\u7b80\u5355\u7684\u7bad\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([0, 1], [0, 1])<\/p>\n<p>ax.arrow(0, 0, 1, 0, head_width=0.05, head_length=0.1, fc=&#39;green&#39;, ec=&#39;green&#39;)<\/p>\n<p>ax.arrow(0, 0, 0, 1, head_width=0.05, head_length=0.1, fc=&#39;green&#39;, ec=&#39;green&#39;)<\/p>\n<p>ax.set_xlim(-0.1, 1.1)<\/p>\n<p>ax.set_ylim(-0.1, 1.1)<\/p>\n<p>ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax.set_title(&#39;Coordinate Axes with Arrow Function&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684Matplotlib\u5e93\u6765\u7ed8\u5236\u5e26\u6709\u7bad\u5934\u7684\u5750\u6807\u8f74\u3002\u901a\u8fc7\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u3001<code>FancyArrowPatch<\/code>\u7c7b\u4ee5\u53ca<code>arrow<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u56fe\u8868\u4e2d\u6dfb\u52a0\u7bad\u5934\uff0c\u4ece\u800c\u589e\u5f3a\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u8868\u73b0\u529b\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u5728\u7ed8\u56fe\u65f6\u80fd\u591f\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u5e26\u7bad\u5934\u7684\u5750\u6807\u8f74\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u5e26\u7bad\u5934\u7684\u5750\u6807\u8f74\u3002\u9996\u5148\uff0c\u5bfc\u5165Matplotlib\u5e76\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\uff0c\u7136\u540e\u5229\u7528<code>quiver<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u7bad\u5934\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u548c\u6807\u7b7e\u6765\u5b8c\u6210\u6574\u4e2a\u7ed8\u56fe\u8fc7\u7a0b\u3002\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\nplt.figure()\n\n# \u7ed8\u5236\u7bad\u5934\nplt.quiver(0, 0, 1, 0, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;r&#39;)  # X\u8f74\u7bad\u5934\nplt.quiver(0, 0, 0, 1, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;b&#39;)  # Y\u8f74\u7bad\u5934\n\n# \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\nplt.xlim(-1, 2)\nplt.ylim(-1, 2)\n\n# \u6dfb\u52a0\u5750\u6807\u8f74\u6807\u7b7e\nplt.xlabel(&#39;X\u8f74&#39;)\nplt.ylabel(&#39;Y\u8f74&#39;)\n\n# \u663e\u793a\u56fe\u5f62\nplt.grid()\nplt.axhline(0, color=&#39;black&#39;,linewidth=0.5, ls=&#39;--&#39;)\nplt.axvline(0, color=&#39;black&#39;,linewidth=0.5, ls=&#39;--&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u5728Python\u4e2d\u6dfb\u52a0\u5750\u6807\u8f74\u7bad\u5934\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528Matplotlib\u5e93\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5982Plotly\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u7ed8\u5236\u5e26\u7bad\u5934\u7684\u5750\u6807\u8f74\u3002\u4f7f\u7528Plotly\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>layout<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6837\u5f0f\u548c\u7bad\u5934\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u7ed8\u56fe\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u5750\u6807\u8f74\u7bad\u5934\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u7bad\u5934\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u7bad\u5934\u6837\u5f0f\u53ef\u4ee5\u901a\u8fc7Matplotlib\u4e2d\u7684<code>Arrow<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u8c03\u6574\u7bad\u5934\u7684\u989c\u8272\u3001\u5bbd\u5ea6\u3001\u957f\u5ea6\u7b49\u5c5e\u6027\uff0c\u751a\u81f3\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u7bad\u5934\u5f62\u72b6\u3002\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u60a8\u7684\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u4e2a\u6027\u5316\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u753b\u5750\u6807\u8f74\u7bad\u5934\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\uff0c\u8bf8\u5982Matplotlib\u3001Plotly\u548cSeaborn\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1082454,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082446"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1082446"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082446\/revisions"}],"predecessor-version":[{"id":1082455,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082446\/revisions\/1082455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082454"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}