{"id":1076582,"date":"2025-01-08T11:54:43","date_gmt":"2025-01-08T03:54:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076582.html"},"modified":"2025-01-08T11:54:45","modified_gmt":"2025-01-08T03:54:45","slug":"python%e5%90%8c%e5%bf%83%e5%9c%86%e5%a6%82%e4%bd%95%e7%bb%99%e5%9c%86%e7%8e%af%e4%b8%8a%e8%89%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076582.html","title":{"rendered":"python\u540c\u5fc3\u5706\u5982\u4f55\u7ed9\u5706\u73af\u4e0a\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181057\/ebb11583-ca55-4451-83b9-b1ecfc886950.webp\" alt=\"python\u540c\u5fc3\u5706\u5982\u4f55\u7ed9\u5706\u73af\u4e0a\u8272\" \/><\/p>\n<p><h3>PYTHON\u540c\u5fc3\u5706\u5982\u4f55\u7ed9\u5706\u73af\u4e0a\u8272<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\u662f\u4e00\u9879\u57fa\u672c\u4f46\u91cd\u8981\u7684\u4efb\u52a1\uff0c<strong>\u4f7f\u7528matplotlib\u5e93\u3001\u5229\u7528plt.Circle\u5bf9\u8c61\u521b\u5efa\u5706\u5f62\u548c\u73af\u5f62\u533a\u57df\u3001\u4f7f\u7528plt.fill_between\u7ed8\u5236\u586b\u5145\u533a\u57df<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\u3002<\/p>\n<\/p>\n<p><h4>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5305\u62ec\u540c\u5fc3\u5706\u73af\u3002\u4e3a\u4e86\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u7684patches\u6a21\u5757\u4e2d\u7684Circle\u5bf9\u8c61\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<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>circle1 = patches.Circle((0.5, 0.5), 0.3, edgecolor=&#39;black&#39;, facecolor=&#39;none&#39;, linewidth=1.5)<\/p>\n<p>circle2 = patches.Circle((0.5, 0.5), 0.2, edgecolor=&#39;black&#39;, facecolor=&#39;blue&#39;, linewidth=1.5)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>ax.add_patch(circle1)<\/p>\n<p>ax.add_patch(circle2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u540c\u5fc3\u5706\uff0c\u5e76\u5206\u522b\u8bbe\u7f6e\u4e86\u5b83\u4eec\u7684\u989c\u8272\u548c\u8fb9\u754c\u3002\u901a\u8fc7\u8bbe\u7f6efacecolor\u53c2\u6570\u4e3a&#39;none&#39;\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ea\u6709\u8fb9\u754c\u7684\u5916\u5706\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6efacecolor\u53c2\u6570\u4e3a\u84dd\u8272\uff0c\u521b\u5efa\u4e00\u4e2a\u586b\u5145\u4e86\u989c\u8272\u7684\u5185\u5706\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u5229\u7528plt.Circle\u5bf9\u8c61\u521b\u5efa\u5706\u5f62\u548c\u73af\u5f62\u533a\u57df<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5706\u5f62\u548c\u73af\u5f62\u533a\u57df\u7684\u989c\u8272\u548c\u6837\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528plt.Circle\u5bf9\u8c61\u3002plt.Circle\u5bf9\u8c61\u662fmatplotlib\u4e2d\u7528\u4e8e\u521b\u5efa\u5706\u5f62\u533a\u57df\u7684\u4e3b\u8981\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u8bbe\u7f6e\u5706\u5f62\u533a\u57df\u7684\u5916\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>circle1 = plt.Circle((0.5, 0.5), 0.3, edgecolor=&#39;black&#39;, facecolor=&#39;none&#39;, linewidth=1.5)<\/p>\n<p>circle2 = plt.Circle((0.5, 0.5), 0.2, edgecolor=&#39;black&#39;, facecolor=&#39;blue&#39;, linewidth=1.5)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>ax.add_artist(circle1)<\/p>\n<p>ax.add_artist(circle2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528plt.Circle\u5bf9\u8c61\u521b\u5efa\u4e86\u4e24\u4e2a\u540c\u5fc3\u5706\uff0c\u5e76\u5206\u522b\u8bbe\u7f6e\u4e86\u5b83\u4eec\u7684\u989c\u8272\u548c\u8fb9\u754c\u3002\u901a\u8fc7\u8bbe\u7f6efacecolor\u53c2\u6570\u4e3a&#39;none&#39;\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ea\u6709\u8fb9\u754c\u7684\u5916\u5706\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6efacecolor\u53c2\u6570\u4e3a\u84dd\u8272\uff0c\u521b\u5efa\u4e00\u4e2a\u586b\u5145\u4e86\u989c\u8272\u7684\u5185\u5706\u3002<\/p>\n<\/p>\n<p><h4>\u4e09\u3001\u4f7f\u7528plt.fill_between\u7ed8\u5236\u586b\u5145\u533a\u57df<\/h4>\n<\/p>\n<p><p>plt.fill_between\u662fmatplotlib\u4e2d\u7528\u4e8e\u7ed8\u5236\u586b\u5145\u533a\u57df\u7684\u51fd\u6570\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u586b\u5145\u4e24\u4e2a\u66f2\u7ebf\u4e4b\u95f4\u7684\u533a\u57df\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u89d2\u5ea6\u8303\u56f4<\/strong><\/h2>\n<p>theta = np.linspace(0, 2*np.pi, 100)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u7ed8\u5236\u586b\u5145\u533a\u57df<\/strong><\/h2>\n<p>r1 = 0.3<\/p>\n<p>r2 = 0.2<\/p>\n<p>x1 = r1 * np.cos(theta) + 0.5<\/p>\n<p>y1 = r1 * np.sin(theta) + 0.5<\/p>\n<p>x2 = r2 * np.cos(theta) + 0.5<\/p>\n<p>y2 = r2 * np.sin(theta) + 0.5<\/p>\n<p>ax.fill_between(x1, y1, x2, facecolor=&#39;blue&#39;, edgecolor=&#39;black&#39;, linewidth=1.5)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528plt.fill_between\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u586b\u5145\u4e86\u989c\u8272\u7684\u73af\u5f62\u533a\u57df\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528numpy\u7684linspace\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u89d2\u5ea6\u8303\u56f4\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u89d2\u5ea6\u6765\u8ba1\u7b97\u4e24\u4e2a\u5706\u7684\u5750\u6807\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528plt.fill_between\u51fd\u6570\u586b\u5145\u4e24\u4e2a\u5706\u4e4b\u95f4\u7684\u533a\u57df\u3002<\/p>\n<\/p>\n<p><h4>\u56db\u3001\u7efc\u5408\u793a\u4f8b\uff1a\u7ed8\u5236\u591a\u4e2a\u540c\u5fc3\u5706\u73af\u5e76\u4e0a\u8272<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u5982\u4f55\u7ed8\u5236\u548c\u4e0a\u8272\u591a\u4e2a\u540c\u5fc3\u5706\u73af\uff0c\u6211\u4eec\u5c06\u7ed3\u5408\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u540c\u5fc3\u5706\u73af\u7684\u56fe\u5f62\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u73af\u7684\u534a\u5f84\u548c\u989c\u8272<\/strong><\/h2>\n<p>radii = [0.3, 0.2, 0.1]<\/p>\n<p>colors = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>for radius, color in zip(radii, colors):<\/p>\n<p>    circle = patches.Circle((0.5, 0.5), radius, edgecolor=&#39;black&#39;, facecolor=color, linewidth=1.5)<\/p>\n<p>    ax.add_patch(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2a\u540c\u5fc3\u5706\u73af\uff0c\u5e76\u5206\u522b\u8bbe\u7f6e\u4e86\u5b83\u4eec\u7684\u989c\u8272\u548c\u8fb9\u754c\u3002\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\u548czip\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u6dfb\u52a0\u591a\u4e2a\u540c\u5fc3\u5706\u73af\u3002<\/p>\n<\/p>\n<p><h4>\u4e94\u3001\u8c03\u6574\u5706\u73af\u7684\u900f\u660e\u5ea6\u548c\u8fb9\u754c\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u540c\u5fc3\u5706\u73af\u7684\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574\u5706\u73af\u7684\u900f\u660e\u5ea6\u548c\u8fb9\u754c\u6837\u5f0f\u3002Matplotlib\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u8bbe\u7f6e\u8fd9\u4e9b\u5c5e\u6027\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u73af\u7684\u534a\u5f84\u3001\u989c\u8272\u548c\u900f\u660e\u5ea6<\/strong><\/h2>\n<p>radii = [0.3, 0.2, 0.1]<\/p>\n<p>colors = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<p>alphas = [0.5, 0.7, 0.9]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>for radius, color, alpha in zip(radii, colors, alphas):<\/p>\n<p>    circle = patches.Circle((0.5, 0.5), radius, edgecolor=&#39;black&#39;, facecolor=color, linewidth=1.5, alpha=alpha)<\/p>\n<p>    ax.add_patch(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6ealpha\u53c2\u6570\u6765\u8c03\u6574\u5706\u73af\u7684\u900f\u660e\u5ea6\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684alpha\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u4e0d\u540c\u900f\u660e\u5ea6\u7684\u5706\u73af\uff0c\u4ece\u800c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8c03\u6574\u5706\u73af\u7684\u8fb9\u754c\u6837\u5f0f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u533a\u5206\u4e0d\u540c\u7684\u5706\u73af\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u73af\u7684\u534a\u5f84\u3001\u989c\u8272\u3001\u900f\u660e\u5ea6\u548c\u8fb9\u754c\u6837\u5f0f<\/strong><\/h2>\n<p>radii = [0.3, 0.2, 0.1]<\/p>\n<p>colors = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<p>alphas = [0.5, 0.7, 0.9]<\/p>\n<p>linestyles = [&#39;-&#39;, &#39;--&#39;, &#39;-.&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>for radius, color, alpha, linestyle in zip(radii, colors, alphas, linestyles):<\/p>\n<p>    circle = patches.Circle((0.5, 0.5), radius, edgecolor=&#39;black&#39;, facecolor=color, linewidth=1.5, alpha=alpha, linestyle=linestyle)<\/p>\n<p>    ax.add_patch(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6elinestyle\u53c2\u6570\u6765\u8c03\u6574\u5706\u73af\u7684\u8fb9\u754c\u6837\u5f0f\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684linestyle\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u4e0d\u540c\u8fb9\u754c\u6837\u5f0f\u7684\u5706\u73af\uff0c\u4ece\u800c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>\u516d\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u540c\u5fc3\u5706\u73af\u7684\u56fe\u5f62\u66f4\u52a0\u6613\u4e8e\u7406\u89e3\u548c\u89e3\u91ca\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\u3002Matplotlib\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\uff0c\u5305\u62ectext\u548cannotate\u51fd\u6570\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u73af\u7684\u534a\u5f84\u3001\u989c\u8272\u3001\u900f\u660e\u5ea6\u548c\u8fb9\u754c\u6837\u5f0f<\/strong><\/h2>\n<p>radii = [0.3, 0.2, 0.1]<\/p>\n<p>colors = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<p>alphas = [0.5, 0.7, 0.9]<\/p>\n<p>linestyles = [&#39;-&#39;, &#39;--&#39;, &#39;-.&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>for radius, color, alpha, linestyle in zip(radii, colors, alphas, linestyles):<\/p>\n<p>    circle = patches.Circle((0.5, 0.5), radius, edgecolor=&#39;black&#39;, facecolor=color, linewidth=1.5, alpha=alpha, linestyle=linestyle)<\/p>\n<p>    ax.add_patch(circle)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca<\/strong><\/h2>\n<p>ax.text(0.5, 0.8, &#39;Outer Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;red&#39;)<\/p>\n<p>ax.text(0.5, 0.6, &#39;Middle Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;green&#39;)<\/p>\n<p>ax.text(0.5, 0.4, &#39;Inner Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4f7f\u7528text\u51fd\u6570\u5728\u540c\u5fc3\u5706\u73af\u7684\u56fe\u5f62\u4e0a\u6dfb\u52a0\u4e86\u6807\u7b7e\u3002\u901a\u8fc7\u8bbe\u7f6ehorizontalalignment\u548cverticalalignment\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u6807\u7b7e\u7684\u4f4d\u7f6e\u3002\u901a\u8fc7\u8bbe\u7f6efontsize\u548ccolor\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u6807\u7b7e\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>\u4e03\u3001\u4fdd\u5b58\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed8\u5236\u597d\u7684\u540c\u5fc3\u5706\u73af\u56fe\u5f62\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002Matplotlib\u63d0\u4f9b\u4e86savefig\u51fd\u6570\u6765\u4fdd\u5b58\u56fe\u5f62\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u533a\u57df<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u73af\u7684\u534a\u5f84\u3001\u989c\u8272\u3001\u900f\u660e\u5ea6\u548c\u8fb9\u754c\u6837\u5f0f<\/strong><\/h2>\n<p>radii = [0.3, 0.2, 0.1]<\/p>\n<p>colors = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<p>alphas = [0.5, 0.7, 0.9]<\/p>\n<p>linestyles = [&#39;-&#39;, &#39;--&#39;, &#39;-.&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u6dfb\u52a0\u540c\u5fc3\u5706\u73af<\/strong><\/h2>\n<p>for radius, color, alpha, linestyle in zip(radii, colors, alphas, linestyles):<\/p>\n<p>    circle = patches.Circle((0.5, 0.5), radius, edgecolor=&#39;black&#39;, facecolor=color, linewidth=1.5, alpha=alpha, linestyle=linestyle)<\/p>\n<p>    ax.add_patch(circle)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca<\/strong><\/h2>\n<p>ax.text(0.5, 0.8, &#39;Outer Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;red&#39;)<\/p>\n<p>ax.text(0.5, 0.6, &#39;Middle Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;green&#39;)<\/p>\n<p>ax.text(0.5, 0.4, &#39;Inner Circle&#39;, horizontalalignment=&#39;center&#39;, verticalalignment=&#39;center&#39;, fontsize=12, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;concentric_circles.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4f7f\u7528savefig\u51fd\u6570\u5c06\u540c\u5fc3\u5706\u73af\u56fe\u5f62\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u683c\u5f0f\uff0c\u4ee5\u4fbf\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u4e0d\u540c\u7684\u6587\u4ef6\u7c7b\u578b\uff0c\u5982PNG\u3001PDF\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528plt.Circle\u5bf9\u8c61\u521b\u5efa\u5706\u5f62\u548c\u73af\u5f62\u533a\u57df\uff0c\u4f7f\u7528plt.fill_between\u7ed8\u5236\u586b\u5145\u533a\u57df\uff0c\u5e76\u901a\u8fc7\u8c03\u6574\u900f\u660e\u5ea6\u3001\u8fb9\u754c\u6837\u5f0f\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u91ca\u7b49\u65b9\u5f0f\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u7ed8\u5236\u597d\u7684\u56fe\u5f62\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u548c\u5206\u4eab\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u540c\u5fc3\u5706\u5e76\u4e3a\u5176\u4e0a\u8272\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u540c\u5fc3\u5706\u5e76\u4e3a\u5176\u4e0a\u8272\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip install matplotlib<\/code>\u6765\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.Circle<\/code>\u51fd\u6570\u521b\u5efa\u5706\u5f62\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4e0d\u540c\u7684\u534a\u5f84\u548c\u989c\u8272\uff0c\u6700\u540e\u5c06\u8fd9\u4e9b\u5706\u5f62\u6dfb\u52a0\u5230\u56fe\u5f62\u4e2d\u5e76\u663e\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u6574\u540c\u5fc3\u5706\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>alpha<\/code>\u53c2\u6570\u6765\u8c03\u6574\u900f\u660e\u5ea6\u3002\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u5706\u5f62\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\u503c\uff0c\u53ef\u4ee5\u4f7f\u540c\u5fc3\u5706\u7684\u89c6\u89c9\u6548\u679c\u66f4\u52a0\u4e30\u5bcc\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8f83\u5927\u7684\u5706\u5f62\u5e76\u8bbe\u7f6e\u4e3a\u534a\u900f\u660e\uff0c\u7136\u540e\u5728\u5176\u4e0a\u6dfb\u52a0\u66f4\u5c0f\u7684\u4e0d\u900f\u660e\u5706\u5f62\uff0c\u4ee5\u7a81\u51fa\u663e\u793a\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u540c\u5fc3\u5706\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5706\u7684\u53e0\u52a0\u95ee\u9898\uff1f<\/strong><br \/>\u5f53\u7ed8\u5236\u540c\u5fc3\u5706\u65f6\uff0c\u5706\u7684\u53e0\u52a0\u4f1a\u5bfc\u81f4\u989c\u8272\u6df7\u5408\u6216\u89c6\u89c9\u6548\u679c\u4e0d\u4f73\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6bcf\u4e2a\u5706\u7684<code>zorder<\/code>\u5c5e\u6027\u6765\u63a7\u5236\u7ed8\u5236\u987a\u5e8f\u3002\u8f83\u5c0f\u7684\u5706\u5f62\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u8f83\u5927\u7684\u5706\u5f62\u4e4b\u4e0a\uff0c\u4ece\u800c\u786e\u4fdd\u5b83\u4eec\u4e0d\u4f1a\u88ab\u906e\u6321\u3002\u540c\u65f6\uff0c\u9002\u5f53\u5730\u9009\u62e9\u989c\u8272\u548c\u900f\u660e\u5ea6\u4e5f\u53ef\u4ee5\u6539\u5584\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u540c\u5fc3\u5706\u5982\u4f55\u7ed9\u5706\u73af\u4e0a\u8272 \u5728Python\u4e2d\u7ed8\u5236\u548c\u4e0a\u8272\u540c\u5fc3\u5706\u73af\u662f\u4e00\u9879\u57fa\u672c\u4f46\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u4f7f\u7528matplot [&hellip;]","protected":false},"author":3,"featured_media":1076592,"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\/1076582"}],"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=1076582"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076582\/revisions"}],"predecessor-version":[{"id":1076595,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076582\/revisions\/1076595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076592"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}