{"id":936923,"date":"2024-12-26T19:39:49","date_gmt":"2024-12-26T11:39:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936923.html"},"modified":"2024-12-26T19:39:51","modified_gmt":"2024-12-26T11:39:51","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e9%9d%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936923.html","title":{"rendered":"python\u5982\u4f55\u753b\u9776"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073000\/1584ca16-54cd-4494-a7db-e223a1bb1a0b.webp\" alt=\"python\u5982\u4f55\u753b\u9776\" \/><\/p>\n<p><p> <strong>Python\u7ed8\u5236\u9776\u56fe\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\uff0c\u901a\u8fc7\u521b\u5efa\u540c\u5fc3\u5706\u3001\u8bbe\u7f6e\u989c\u8272\u6e10\u53d8\u548c\u8c03\u6574\u5706\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u3002Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u3001\u4f7f\u7528\u7b80\u5355\u4e14\u7075\u6d3b\u3001\u652f\u6301\u591a\u79cd\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u7ed8\u5236\u9776\u56fe\u65f6\uff0cMatplotlib\u5e93\u662f\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u800c\u4e14\u76f8\u5bf9\u5bb9\u6613\u4e0a\u624b\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u7684\u5706\u5f62\u7ed8\u56fe\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u521b\u5efa\u540c\u5fc3\u5706\uff0c\u4ece\u800c\u5f62\u6210\u4e00\u4e2a\u9776\u56fe\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u8c03\u6574\u989c\u8272\u548c\u534a\u5f84\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u9776\u56fe\u66f4\u52a0\u751f\u52a8\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Matplotlib\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u9776\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u7b80\u4ecb<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5e7f\u6cdb\u7528\u4e8e\u751f\u6210\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u3002\u5b83\u5177\u6709\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u7ed8\u56fe\u80fd\u529b\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u7ed8\u56fe\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528Matplotlib\uff0c\u9996\u5148\u9700\u8981\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\u6765\u521b\u5efa\u57fa\u672c\u7684\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4]<\/p>\n<p>y = [10, 20, 25, 30]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Matplotlib\u7684\u57fa\u672c\u7ec4\u4ef6<\/li>\n<\/ol>\n<p><p>Matplotlib\u7684\u56fe\u5f62\u7531\u591a\u4e2a\u7ec4\u4ef6\u7ec4\u6210\uff0c\u5305\u62ecFigure\u3001Axes\u3001Axis\u7b49\u3002Figure\u662f\u6574\u4e2a\u56fe\u5f62\u7684\u5bb9\u5668\uff0cAxes\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u7ed8\u56fe\u533a\u57df\uff0cAxis\u662f\u5750\u6807\u8f74\u3002\u901a\u8fc7\u8fd9\u4e9b\u7ec4\u4ef6\u7684\u7ec4\u5408\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5982\u4f55\u4f7f\u7528MATPLOTLIB\u7ed8\u5236\u9776\u56fe<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u9776\u56fe\u7684\u5173\u952e\u5728\u4e8e\u7ed8\u5236\u591a\u4e2a\u540c\u5fc3\u5706\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5706\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684Circle\u5bf9\u8c61\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u540c\u5fc3\u5706<\/li>\n<\/ol>\n<p><p>\u8981\u7ed8\u5236\u540c\u5fc3\u5706\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684Circle\u5bf9\u8c61\u3002Circle\u5bf9\u8c61\u9700\u8981\u6307\u5b9a\u5706\u5fc3\u548c\u534a\u5f84\u3002\u901a\u8fc7\u5728\u540c\u4e00\u4f4d\u7f6e\u7ed8\u5236\u591a\u4e2aCircle\u5bf9\u8c61\uff0c\u5e76\u8c03\u6574\u5b83\u4eec\u7684\u534a\u5f84\u548c\u989c\u8272\uff0c\u53ef\u4ee5\u521b\u5efa\u9776\u56fe\u6548\u679c\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<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u5706\u5fc3\u4f4d\u7f6e<\/strong><\/h2>\n<p>center = (0, 0)<\/p>\n<h2><strong>\u4e0d\u540c\u534a\u5f84\u7684\u540c\u5fc3\u5706<\/strong><\/h2>\n<p>radii = [1, 2, 3, 4, 5]<\/p>\n<p>colors = [&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;, &#39;yellow&#39;, &#39;black&#39;]<\/p>\n<p>for radius, color in zip(radii, colors):<\/p>\n<p>    circle = plt.Circle(center, radius, color=color, fill=True)<\/p>\n<p>    ax.add_artist(circle)<\/p>\n<p>ax.set_xlim(-6, 6)<\/p>\n<p>ax.set_ylim(-6, 6)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;, &#39;box&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bbe\u7f6e\u989c\u8272\u6e10\u53d8<\/li>\n<\/ol>\n<p><p>\u5728\u9776\u56fe\u4e2d\uff0c\u4e0d\u540c\u7684\u5706\u73af\u901a\u5e38\u5177\u6709\u4e0d\u540c\u7684\u989c\u8272\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u989c\u8272\u53c2\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002Matplotlib\u652f\u6301\u591a\u79cd\u989c\u8272\u8868\u793a\u65b9\u5f0f\uff0c\u5982\u5b57\u7b26\u4e32\u3001RGB\u503c\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u8c03\u6574\u5706\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8c03\u6574Circle\u5bf9\u8c61\u7684\u534a\u5f84\u53c2\u6570\uff0c\u53ef\u4ee5\u6539\u53d8\u5706\u7684\u5927\u5c0f\u3002\u901a\u8fc7\u8c03\u6574\u5706\u5fc3\u5750\u6807\uff0c\u53ef\u4ee5\u6539\u53d8\u5706\u7684\u4f4d\u7f6e\u3002\u901a\u5e38\uff0c\u9776\u56fe\u7684\u5706\u5fc3\u4f4d\u4e8e\u5750\u6807\u7cfb\u7684\u539f\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u9776\u56fe\u7684\u9ad8\u7ea7\u81ea\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u9776\u56fe\u7ed8\u5236\u5916\uff0cMatplotlib\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u56fe\u5f62\u66f4\u52a0\u751f\u52a8\u548c\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6dfb\u52a0\u7f51\u683c\u548c\u6807\u7b7e<\/li>\n<\/ol>\n<p><p>\u7f51\u683c\u548c\u6807\u7b7e\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u56fe\u5f62\u6570\u636e\u3002\u5728\u9776\u56fe\u4e2d\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5706\u73af\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.grid(True)<\/p>\n<p>ax.set_xticks(np.arange(-5, 6, 1))<\/p>\n<p>ax.set_yticks(np.arange(-5, 6, 1))<\/p>\n<p>ax.set_xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u989c\u8272\u6620\u5c04<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u60f3\u8981\u66f4\u591a\u7684\u989c\u8272\u9009\u62e9\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u989c\u8272\u6620\u5c04\uff08colormap\uff09\u529f\u80fd\u3002\u901a\u8fc7\u5c06\u989c\u8272\u6620\u5c04\u5e94\u7528\u4e8e\u5706\u73af\uff0c\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u6e10\u53d8\u6548\u679c\u7684\u9776\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.cm as cm<\/p>\n<p>colors = cm.viridis(np.linspace(0, 1, len(radii)))<\/p>\n<p>for radius, color in zip(radii, colors):<\/p>\n<p>    circle = plt.Circle(center, radius, color=color, fill=True)<\/p>\n<p>    ax.add_artist(circle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u7ed8\u56fe\u5e93\u7684\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cPython\u8fd8\u6709\u5176\u4ed6\u7ed8\u56fe\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u9776\u56fe\uff0c\u5982Seaborn\u3001Plotly\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u7279\u70b9\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>Seaborn<\/li>\n<\/ol>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u66f4\u7b80\u4fbf\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u867d\u7136Seaborn\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u7ed8\u5236\u9776\u56fe\uff0c\u4f46\u53ef\u4ee5\u4e0eMatplotlib\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>Plotly<\/li>\n<\/ol>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u751f\u6210\u52a8\u6001\u56fe\u5f62\u3002\u76f8\u6bd4\u4e8eMatplotlib\uff0cPlotly\u66f4\u9002\u5408\u751f\u6210\u4ea4\u4e92\u5f0f\u7684\u9776\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7ed8\u5236\u9776\u56fe\u662f\u4e00\u9879\u6709\u8da3\u4e14\u5b9e\u7528\u7684\u6280\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u76f4\u89c2\u5c55\u793a\u6570\u636e\u65f6\u3002\u901a\u8fc7Matplotlib\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u540c\u5fc3\u5706\u3001\u8bbe\u7f6e\u989c\u8272\u6e10\u53d8\u548c\u8c03\u6574\u5706\u7684\u5927\u5c0f\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u9776\u56fe\u8fd8\u662f\u590d\u6742\u7684\u81ea\u5b9a\u4e49\u56fe\u5f62\uff0cMatplotlib\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u6b64\u5916\uff0c\u7ed3\u5408Seaborn\u548cPlotly\u7b49\u5176\u4ed6\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u751f\u6210\u66f4\u4e3a\u4e30\u5bcc\u591a\u6837\u7684\u56fe\u5f62\u6548\u679c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u7684\u7ed8\u56fe\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u9776\u5b50\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u9776\u5b50\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install matplotlib<\/code>\u5b8c\u6210\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>plt.Circle<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u9776\u5b50\u7684\u4e0d\u540c\u5c42\u6b21\uff0c\u5e76\u901a\u8fc7<code>plt.plot<\/code>\u6765\u7ed8\u5236\u9776\u5b50\u7684\u5916\u5708\u548c\u4e2d\u5fc3\u70b9\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u7ed8\u5236\u9776\u5b50\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cSeaborn\u548cPygame\u4e5f\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002Seaborn\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u56fe\u5f62\u7684\u7ed8\u5236\uff0c\u800cPygame\u5219\u9002\u5408\u5236\u4f5c\u6e38\u620f\u4e2d\u7684\u9776\u5b50\u6548\u679c\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u8ba9\u7ed8\u5236\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u7ed8\u5236\u9776\u5b50\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u53c2\u6570\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u9776\u5b50\u65f6\uff0c\u989c\u8272\u3001\u534a\u5f84\u548c\u7ebf\u5bbd\u662f\u91cd\u8981\u53c2\u6570\u3002\u9009\u62e9\u9c9c\u8273\u7684\u989c\u8272\u53ef\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\uff0c\u5408\u7406\u7684\u534a\u5f84\u8bbe\u7f6e\u53ef\u4ee5\u4f7f\u9776\u5b50\u66f4\u5177\u5c42\u6b21\u611f\uff0c\u800c\u7ebf\u5bbd\u5219\u5f71\u54cd\u9776\u5b50\u7684\u6574\u4f53\u7f8e\u89c2\u7a0b\u5ea6\u3002\u5408\u7406\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\uff0c\u80fd\u8ba9\u4f60\u7684\u9776\u5b50\u66f4\u52a0\u5438\u5f15\u773c\u7403\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed8\u5236\u9776\u56fe\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\uff0c\u901a\u8fc7\u521b\u5efa\u540c\u5fc3\u5706\u3001\u8bbe\u7f6e\u989c\u8272\u6e10\u53d8\u548c\u8c03\u6574\u5706\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u3002Matp [&hellip;]","protected":false},"author":3,"featured_media":936926,"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\/936923"}],"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=936923"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936923\/revisions"}],"predecessor-version":[{"id":936927,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936923\/revisions\/936927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936926"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}