{"id":981403,"date":"2024-12-27T07:02:22","date_gmt":"2024-12-26T23:02:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981403.html"},"modified":"2024-12-27T07:02:24","modified_gmt":"2024-12-26T23:02:24","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%9c%86plt","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981403.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u5706plt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210352\/1edecfff-57b0-4052-b91b-364f571d4bf3.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u5706plt\" \/><\/p>\n<p><p> <strong>\u7528Python\u7684matplotlib\u5e93\u753b\u5706\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u3001\u4f7f\u7528patches.Circle\u521b\u5efa\u5706\u5f62\u3001\u5c06\u5706\u5f62\u6dfb\u52a0\u5230\u56fe\u5f62\u4e2d\u3001\u663e\u793a\u56fe\u5f62\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u5706\u5f62\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u5706\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\u4ee5\u53capatches\u6a21\u5757\u3002pyplot\u6a21\u5757\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u548c\u63a7\u5236\u56fe\u5f62\u7684\u663e\u793a\uff0c\u800cpatches\u6a21\u5757\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u56fe\u5f62\u548c\u8f74<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u56fe\u5f62\u7684\u7b2c\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u4e00\u4e2a\u8f74\u5bf9\u8c61\u3002\u56fe\u5f62\u5bf9\u8c61\u662f\u753b\u5e03\uff0c\u800c\u8f74\u5bf9\u8c61\u662f\u6211\u4eec\u7ed8\u5236\u5706\u5f62\u7684\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u548c\u4e00\u4e2a\u8f74\u3002<code>fig<\/code>\u662f\u56fe\u5f62\u5bf9\u8c61\uff0c<code>ax<\/code>\u662f\u8f74\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u5706\u5f62\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib.patches\u6a21\u5757\u4e2d\u7684Circle\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u5706\u5f62\u5bf9\u8c61\u3002Circle\u7c7b\u63a5\u53d7\u51e0\u4e2a\u53c2\u6570\uff1a\u4e2d\u5fc3\u70b9\u7684\u5750\u6807\u548c\u534a\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">circle = patches.Circle((0.5, 0.5), 0.2, edgecolor=&#39;r&#39;, facecolor=&#39;none&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e2d\u5fc3\u5728(0.5, 0.5)\uff0c\u534a\u5f84\u4e3a0.2\u7684\u5706\u3002<code>edgecolor<\/code>\u53c2\u6570\u8bbe\u7f6e\u5706\u7684\u8fb9\u6846\u989c\u8272\u4e3a\u7ea2\u8272\uff0c<code>facecolor<\/code>\u8bbe\u7f6e\u4e3a&#39;none&#39;\u8868\u793a\u5706\u5185\u90e8\u662f\u900f\u660e\u7684\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5c06\u5706\u5f62\u6dfb\u52a0\u5230\u8f74\u4e2d<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>add_patch<\/code>\u65b9\u6cd5\u5c06\u521b\u5efa\u7684\u5706\u5f62\u5bf9\u8c61\u6dfb\u52a0\u5230\u8f74\u5bf9\u8c61\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.add_patch(circle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5c06\u5706\u5f62\u6dfb\u52a0\u5230\u8f74\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8bbe\u7f6e\u8f74\u7684\u9650\u5236\u548c\u6bd4\u4f8b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u5706\u5f62\u5728\u56fe\u5f62\u4e2d\u6b63\u786e\u663e\u793a\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u8f74\u7684\u9650\u5236\u548c\u6bd4\u4f8b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>set_xlim<\/code>\u548c<code>set_ylim<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u8f74\u7684\u9650\u5236\uff0c\u4ee5\u53ca<code>set_aspect<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u51e0\u884c\u4ee3\u7801\u5c06\u8f74\u7684\u9650\u5236\u8bbe\u7f6e\u4e3a0\u52301\uff0c\u5e76\u5c06\u6bd4\u4f8b\u8bbe\u7f6e\u4e3a1:1\uff0c\u4ee5\u786e\u4fdd\u5706\u5f62\u4e0d\u4f1a\u53d8\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u663e\u793a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u663e\u793a\u56fe\u5f62\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>show<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5c06\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\uff0c\u663e\u793a\u6211\u4eec\u521b\u5efa\u7684\u5706\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cmatplotlib\u5e93\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u5706\u5f62\u3002matplotlib\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u66f4\u590d\u6742\u7684\u56fe\u5f62\u548c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u7ed8\u5236\u591a\u4e2a\u5706\u5f62<\/p>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u540c\u4e00\u4e2a\u56fe\u5f62\u4e2d\u7ed8\u5236\u591a\u4e2a\u5706\u5f62\uff0c\u53ea\u9700\u91cd\u590d\u521b\u5efa\u5706\u5f62\u5bf9\u8c61\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u8f74\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">circle1 = patches.Circle((0.3, 0.3), 0.1, edgecolor=&#39;b&#39;, facecolor=&#39;none&#39;)<\/p>\n<p>circle2 = patches.Circle((0.7, 0.7), 0.1, edgecolor=&#39;g&#39;, facecolor=&#39;none&#39;)<\/p>\n<p>ax.add_patch(circle1)<\/p>\n<p>ax.add_patch(circle2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u989d\u5916\u6dfb\u52a0\u4e24\u4e2a\u5706\u5f62\uff0c\u4e00\u4e2a\u84dd\u8272\u8fb9\u6846\u7684\u5706\u548c\u4e00\u4e2a\u7eff\u8272\u8fb9\u6846\u7684\u5706\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u7ed8\u5236\u5706\u5f62<\/p>\n<\/p>\n<p><p>\u9664\u4e86matplotlib\uff0cPython\u8fd8\u6709\u5176\u4ed6\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5706\u5f62\uff0c\u4f8b\u5982\uff1aturtle\u5e93\u548cPIL\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528turtle\u5e93<\/li>\n<\/ol>\n<p><p><code>turtle<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8eTkinter\u7684Python\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7f16\u7a0b\u3002\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u5706\u5f62\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p>t = turtle.Turtle()<\/p>\n<p>t.circle(50)<\/p>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u4e86\u4e00\u4e2a\u534a\u5f84\u4e3a50\u50cf\u7d20\u7684\u5706\u5f62\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528PIL\u5e93<\/li>\n<\/ol>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5904\u7406\u56fe\u50cf\u7684\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u7f16\u8f91\u56fe\u50cf\u3002\u4f7f\u7528PIL\u5e93\u7ed8\u5236\u5706\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<p>image = Image.new(&#39;RGB&#39;, (100, 100), &#39;white&#39;)<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<p>draw.ellipse((25, 25, 75, 75), outline=&#39;black&#39;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528PIL\u5e93\u521b\u5efa\u4e86\u4e00\u5e45\u56fe\u50cf\uff0c\u5e76\u7ed8\u5236\u4e86\u4e00\u4e2a\u5706\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7ed8\u5236\u5706\u5f62\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528matplotlib\u5e93\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aCircle\u5bf9\u8c61\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u8f74\u4e2d\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7ed8\u5236\u51fa\u4e00\u4e2a\u5706\u5f62\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5982turtle\u548cPIL\u6765\u7ed8\u5236\u5706\u5f62\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u719f\u6089\u7a0b\u5ea6\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u53ef\u89c6\u5316\u548c\u56fe\u5f62\u5904\u7406\u65b9\u9762\u53d6\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5706\u5f62\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>plt<\/code>\u6a21\u5757\u8f7b\u677e\u7ed8\u5236\u5706\u5f62\u56fe\u3002\u901a\u8fc7<code>plt.Circle<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5706\u5fc3\u548c\u534a\u5f84\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u7ed8\u56fe\u8f74\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\nfig, ax = plt.subplots()\n\n# \u521b\u5efa\u4e00\u4e2a\u5706\u5f62\ncircle = plt.Circle((0.5, 0.5), 0.3, color=&#39;blue&#39;, fill=True)\n\n# \u6dfb\u52a0\u5706\u5f62\u5230\u8f74\u4e2d\nax.add_artist(circle)\n\n# \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\n\n# \u663e\u793a\u56fe\u5f62\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u5b9e\u5fc3\u5706\u3002<\/p>\n<p><strong>\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5e93\u624d\u80fd\u7ed8\u5236\u5706\u5f62\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u7ed8\u5236\u5706\u5f62\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install matplotlib<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u56e0\u4e3aMatplotlib\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u4f9d\u8d56\u4e8eNumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u5706\u7684\u989c\u8272\u548c\u8fb9\u6846\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u5706\u5f62\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>plt.Circle<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u989c\u8272\u548c\u8fb9\u6846\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>color<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u586b\u5145\u989c\u8272\uff0c\u4f7f\u7528<code>edgecolor<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\uff0c\u540c\u65f6\u901a\u8fc7<code>linewidth<\/code>\u53c2\u6570\u8c03\u6574\u8fb9\u6846\u7684\u5bbd\u5ea6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">circle = plt.Circle((0.5, 0.5), 0.3, color=&#39;red&#39;, edgecolor=&#39;black&#39;, linewidth=2, fill=True)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7ea2\u8272\u586b\u5145\uff0c\u9ed1\u8272\u8fb9\u6846\u7684\u5706\u5f62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7684matplotlib\u5e93\u753b\u5706\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u3001\u4f7f\u7528patc [&hellip;]","protected":false},"author":3,"featured_media":981409,"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\/981403"}],"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=981403"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981403\/revisions"}],"predecessor-version":[{"id":981413,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981403\/revisions\/981413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981409"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}