{"id":1126314,"date":"2025-01-08T20:00:05","date_gmt":"2025-01-08T12:00:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126314.html"},"modified":"2025-01-08T20:00:08","modified_gmt":"2025-01-08T12:00:08","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python-plot%e7%9a%84%e5%9b%be%e4%bf%9d%e5%ad%98%e4%b8%8b%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126314.html","title":{"rendered":"\u5982\u4f55\u5c06python plot\u7684\u56fe\u4fdd\u5b58\u4e0b\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090650\/e0b97ad5-7378-4720-aaba-4fda2b2ffccd.webp\" alt=\"\u5982\u4f55\u5c06python plot\u7684\u56fe\u4fdd\u5b58\u4e0b\u6765\" \/><\/p>\n<p><p> <strong>\u5feb\u901f\u56de\u7b54\uff1a<\/strong> <strong>\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u3001\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u683c\u5f0f\u3001\u8bbe\u7f6e\u5206\u8fa8\u7387\u3001\u8c03\u6574\u56fe\u5f62\u5c3a\u5bf8<\/strong>\u3002Python\u7684Matplotlib\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u5f62\u7ed8\u5236\u548c\u4fdd\u5b58\u529f\u80fd\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u683c\u5f0f\uff08\u5982PNG\u3001PDF\u3001SVG\u7b49\uff09\uff0c\u8bbe\u7f6e\u5206\u8fa8\u7387\uff08DPI\uff09\uff0c\u4ee5\u53ca\u8c03\u6574\u56fe\u5f62\u7684\u5c3a\u5bf8\u4ee5\u786e\u4fdd\u4fdd\u5b58\u7684\u56fe\u50cf\u7b26\u5408\u9700\u6c42\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570<\/strong><\/h2>\n<p><p>Matplotlib\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5de5\u5177\u4e4b\u4e00\u3002\u5728\u7ed8\u5236\u5b8c\u56fe\u5f62\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed8\u5236\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff0c\u5e76\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002<strong><code>savefig<\/code>\u51fd\u6570\u662f\u4fdd\u5b58\u56fe\u5f62\u7684\u5173\u952e<\/strong>\uff0c\u5b83\u6709\u591a\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u5b9a\u5236\u56fe\u5f62\u7684\u4fdd\u5b58\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u683c\u5f0f<\/strong><\/h2>\n<p><p>Python\u4e2d\u7684Matplotlib\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<\/p>\n<ul>\n<li>PNG\uff1a\u9002\u7528\u4e8e\u7f51\u9875\u548c\u5e38\u89c4\u56fe\u5f62\u5c55\u793a<\/li>\n<li>PDF\uff1a\u9002\u7528\u4e8e\u6253\u5370\u548c\u9ad8\u8d28\u91cf\u5c55\u793a<\/li>\n<li>SVG\uff1a\u9002\u7528\u4e8e\u7f51\u9875\u548c\u53ef\u7f29\u653e\u56fe\u5f62<\/li>\n<li>EPS\uff1a\u9002\u7528\u4e8e\u4e13\u4e1a\u6392\u7248\u548c\u9ad8\u8d28\u91cf\u5370\u5237<\/li>\n<\/ul>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u683c\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u9ad8\u8d28\u91cf\u7684\u6253\u5370\u8f93\u51fa\uff0c\u53ef\u4ee5\u9009\u62e9PDF\u6216EPS\u683c\u5f0f\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u7f51\u9875\u4e0a\u5c55\u793a\u56fe\u5f62\uff0c\u53ef\u4ee5\u9009\u62e9PNG\u6216SVG\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aPDF<\/p>\n<p>plt.savefig(&#39;my_plot.pdf&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aSVG<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot.svg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u8bbe\u7f6e\u5206\u8fa8\u7387\uff08DPI\uff09<\/strong><\/h2>\n<p><p>\u5206\u8fa8\u7387\uff08DPI\uff0cDots Per Inch\uff09\u662f\u56fe\u5f62\u8d28\u91cf\u7684\u91cd\u8981\u53c2\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMatplotlib\u4f1a\u4f7f\u7528\u4e00\u4e2a\u5408\u7406\u7684DPI\u503c\uff0c\u4f46\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\u3002\u8f83\u9ad8\u7684DPI\u503c\u4f1a\u751f\u6210\u66f4\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\uff0c\u4f46\u6587\u4ef6\u5927\u5c0f\u4e5f\u4f1a\u76f8\u5e94\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6eDPI\u4e3a300<\/p>\n<p>plt.savefig(&#39;my_plot_high_res.png&#39;, dpi=300)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u5f62\u7684DPI\u8bbe\u7f6e\u4e3a300\uff0c\u8fd9\u901a\u5e38\u7528\u4e8e\u9ad8\u8d28\u91cf\u7684\u6253\u5370\u8f93\u51fa\u3002<strong>\u8c03\u6574DPI\u53ef\u4ee5\u663e\u8457\u5f71\u54cd\u56fe\u5f62\u7684\u6e05\u6670\u5ea6<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u8c03\u6574\u56fe\u5f62\u5c3a\u5bf8<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u9700\u8981\u8c03\u6574\u56fe\u5f62\u7684\u5c3a\u5bf8\u4ee5\u9002\u5e94\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>figure<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u56fe\u5f62\u7684\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u5f62\u5c3a\u5bf8\uff08\u5bbd\u5ea6\uff0c\u9ad8\u5ea6\uff09<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_large_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u5f62\u7684\u5c3a\u5bf8\u8bbe\u7f6e\u4e3a10\u82f1\u5bf8\u5bbd\u548c6\u82f1\u5bf8\u9ad8\u3002\u8fd9\u5728\u9700\u8981\u7279\u5b9a\u5c3a\u5bf8\u7684\u56fe\u5f62\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5d4c\u5165\u62a5\u544a\u6216\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4fdd\u5b58\u77e2\u91cf\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u77e2\u91cf\u56fe\u5f62\u683c\u5f0f\uff08\u5982SVG\u548cPDF\uff09\u5177\u6709\u53ef\u7f29\u653e\u7684\u4f18\u70b9\uff0c\u5373\u5728\u653e\u5927\u65f6\u4e0d\u4f1a\u5931\u771f\u3002\u8fd9\u5bf9\u4e8e\u9ad8\u8d28\u91cf\u7684\u6253\u5370\u548c\u5c55\u793a\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aSVG\u683c\u5f0f<\/p>\n<p>plt.savefig(&#39;my_plot_vector.svg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SVG\u683c\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u7f51\u9875\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u6839\u636e\u663e\u793a\u8bbe\u5907\u7684\u5206\u8fa8\u7387\u81ea\u52a8\u8c03\u6574\u5927\u5c0f\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4fdd\u5b58\u591a\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed8\u5236\u548c\u4fdd\u5b58\u5305\u542b\u591a\u4e2a\u5b50\u56fe\u7684\u590d\u6742\u56fe\u5f62\u3002Matplotlib\u7684<code>subplot<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56fe\u5f62\u548c\u5b50\u56fe<\/p>\n<p>fig, axs = plt.subplots(2, 2)<\/p>\n<h2><strong>\u7ed8\u5236\u5b50\u56fe<\/strong><\/h2>\n<p>axs[0, 0].plot(x, y)<\/p>\n<p>axs[0, 1].plot(y, x)<\/p>\n<p>axs[1, 0].plot(x, [i2 for i in x])<\/p>\n<p>axs[1, 1].plot(y, [i0.5 for i in y])<\/p>\n<h2><strong>\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd<\/strong><\/h2>\n<p>plt.tight_layout()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_subplots.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u56db\u4e2a\u5b50\u56fe\u7684\u56fe\u5f62\uff0c\u5e76\u4f7f\u7528<code>tight_layout<\/code>\u51fd\u6570\u8c03\u6574\u4e86\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4ee5\u907f\u514d\u91cd\u53e0\u3002<strong>\u4fdd\u5b58\u5305\u542b\u591a\u4e2a\u5b50\u56fe\u7684\u56fe\u5f62\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u5b50\u56fe\u90fd\u6e05\u6670\u53ef\u89c1<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4fdd\u5b58\u5e26\u6709\u6ce8\u91ca\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u9700\u8981\u5728\u56fe\u5f62\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u89e3\u91ca\u6570\u636e\u3002Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6ce8\u91ca\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u6ce8\u91ca\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u56fe\u5f62<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6ce8\u91ca<\/strong><\/h2>\n<p>plt.annotate(&#39;Prime numbers&#39;, xy=(3, 5), xytext=(4, 7),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_annotated_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u56fe\u5f62\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6ce8\u91ca\uff0c\u5e76\u7528\u7bad\u5934\u6307\u5411\u7279\u5b9a\u7684\u6570\u636e\u70b9\u3002<strong>\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u56fe\u5f62\u4e2d\u7684\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4fdd\u5b58\u5e26\u6709\u56fe\u4f8b\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u56fe\u4f8b\u662f\u56fe\u5f62\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\uff0c\u5c24\u5176\u662f\u5728\u7ed8\u5236\u591a\u6761\u66f2\u7ebf\u6216\u591a\u4e2a\u6570\u636e\u7cfb\u5217\u65f6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>legend<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u56fe\u4f8b\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u591a\u6761\u66f2\u7ebf<\/p>\n<p>plt.plot(x, y, label=&#39;Prime numbers&#39;)<\/p>\n<p>plt.plot(x, [i2 for i in x], label=&#39;Squares&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot_with_legend.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed8\u5236\u4e86\u4e24\u6761\u66f2\u7ebf\uff0c\u5e76\u6dfb\u52a0\u4e86\u56fe\u4f8b\u3002<strong>\u56fe\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u533a\u5206\u4e0d\u540c\u7684\u6570\u636e\u7cfb\u5217<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4fdd\u5b58\u5e26\u6709\u6807\u9898\u548c\u6807\u7b7e\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u6807\u9898\u548c\u6807\u7b7e\u662f\u56fe\u5f62\u4e2d\u91cd\u8981\u7684\u5143\u7d20\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u56fe\u5f62\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>title<\/code>\u3001<code>xlabel<\/code>\u548c<code>ylabel<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u56fe\u5f62<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Prime Numbers&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot_with_title_and_labels.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u6807\u9898\u548c\u8f74\u6807\u7b7e\u3002<strong>\u6807\u9898\u548c\u6807\u7b7e\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u7406\u89e3\u56fe\u5f62\u7684\u542b\u4e49<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4fdd\u5b58\u5e26\u6709\u7f51\u683c\u7ebf\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u7f51\u683c\u7ebf\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u51c6\u786e\u5730\u8bfb\u53d6\u56fe\u5f62\u4e2d\u7684\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>grid<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u7f51\u683c\u7ebf\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u56fe\u5f62<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u7f51\u683c\u7ebf<\/strong><\/h2>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot_with_grid.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u7f51\u683c\u7ebf\u3002<strong>\u7f51\u683c\u7ebf\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u5bb9\u6613\u5730\u8bfb\u53d6\u6570\u636e\u70b9<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u4fdd\u5b58\u5e26\u6709\u81ea\u5b9a\u4e49\u6837\u5f0f\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>Matplotlib\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49\u56fe\u5f62\u7684\u6837\u5f0f\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>style.use<\/code>\u51fd\u6570\u6765\u5e94\u7528\u9884\u5b9a\u4e49\u7684\u6837\u5f0f\uff0c\u6216\u8005\u521b\u5efa\u81ea\u5df1\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e94\u7528\u9884\u5b9a\u4e49\u6837\u5f0f<\/p>\n<p>plt.style.use(&#39;ggplot&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_styled_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e94\u7528\u4e86<code>ggplot<\/code>\u6837\u5f0f\uff0c\u8fd9\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6837\u5f0f\u3002<strong>\u81ea\u5b9a\u4e49\u6837\u5f0f\u53ef\u4ee5\u4f7f\u56fe\u5f62\u66f4\u7f8e\u89c2\u548c\u4e13\u4e1a<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e8c\u3001\u4fdd\u5b58\u5e26\u6709\u989c\u8272\u6761\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u5728\u7ed8\u5236\u70ed\u56fe\u6216\u5176\u4ed6\u9700\u8981\u989c\u8272\u6761\u7684\u56fe\u5f62\u65f6\uff0c\u989c\u8272\u6761\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u5143\u7d20\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>colorbar<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u989c\u8272\u6761\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(10, 10)<\/p>\n<h2><strong>\u7ed8\u5236\u70ed\u56fe<\/strong><\/h2>\n<p>plt.imshow(data, cmap=&#39;hot&#39;, interpolation=&#39;nearest&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u989c\u8272\u6761<\/strong><\/h2>\n<p>plt.colorbar()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot_with_colorbar.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed8\u5236\u4e86\u4e00\u4e2a\u70ed\u56fe\uff0c\u5e76\u6dfb\u52a0\u4e86\u989c\u8272\u6761\u3002<strong>\u989c\u8272\u6761\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u6570\u636e\u7684\u8303\u56f4\u548c\u5206\u5e03<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e09\u3001\u4fdd\u5b58\u52a8\u6001\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4fdd\u5b58\u52a8\u6001\u56fe\u5f62\uff0c\u5982\u52a8\u753b\u3002Matplotlib\u7684<code>animation<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u548c\u4fdd\u5b58\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.animation as animation<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 2*np.pi, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u51fd\u6570<\/strong><\/h2>\n<p>def init():<\/p>\n<p>    ax.set_xlim(0, 2*np.pi)<\/p>\n<p>    ax.set_ylim(-1, 1)<\/p>\n<p>    return ln,<\/p>\n<h2><strong>\u66f4\u65b0\u51fd\u6570<\/strong><\/h2>\n<p>def update(frame):<\/p>\n<p>    ln.set_data(x[:frame], y[:frame])<\/p>\n<p>    return ln,<\/p>\n<h2><strong>\u521b\u5efa\u52a8\u753b<\/strong><\/h2>\n<p>ln, = plt.plot([], [], &#39;r-&#39;)<\/p>\n<p>ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)<\/p>\n<h2><strong>\u4fdd\u5b58\u52a8\u753b<\/strong><\/h2>\n<p>ani.save(&#39;my_animation.gif&#39;, writer=&#39;imagemagick&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aGIF\u683c\u5f0f\u3002<strong>\u52a8\u753b\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u968f\u65f6\u95f4\u53d8\u5316\u7684\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u56db\u3001\u4fdd\u5b58\u4ea4\u4e92\u5f0f\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4fdd\u5b58\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002Matplotlib\u4e0ePlotly\u7b49\u5e93\u53ef\u4ee5\u751f\u6210\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u5e76\u4fdd\u5b58\u4e3aHTML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\n<h2><strong>\u7ed8\u5236\u4ea4\u4e92\u5f0f\u56fe\u5f62<\/strong><\/h2>\n<p>fig = px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4ea4\u4e92\u5f0f\u56fe\u5f62<\/strong><\/h2>\n<p>fig.write_html(&#39;my_interactive_plot.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Plotly\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u6563\u70b9\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3aHTML\u6587\u4ef6\u3002<strong>\u4ea4\u4e92\u5f0f\u56fe\u5f62\u53ef\u4ee5\u63d0\u4f9b\u66f4\u4e30\u5bcc\u7684\u7528\u6237\u4f53\u9a8c<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e94\u3001\u4fdd\u5b58\u5e26\u6709\u81ea\u5b9a\u4e49\u6ce8\u91ca\u548c\u6807\u8bb0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5728\u56fe\u5f62\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6ce8\u91ca\u548c\u6807\u8bb0\uff0c\u4ee5\u5f3a\u8c03\u7279\u5b9a\u7684\u6570\u636e\u70b9\u3002Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6ce8\u91ca\u548c\u6807\u8bb0\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u56fe\u5f62<\/p>\n<p>plt.plot(x, y, marker=&#39;o&#39;, linestyle=&#39;--&#39;, color=&#39;r&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6ce8\u91ca<\/strong><\/h2>\n<p>for i, txt in enumerate(y):<\/p>\n<p>    plt.annotate(txt, (x[i], y[i]))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_custom_annotated_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0a\u6dfb\u52a0\u4e86\u6ce8\u91ca\u3002<strong>\u81ea\u5b9a\u4e49\u6ce8\u91ca\u548c\u6807\u8bb0\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u6e05\u6670\u5730\u7406\u89e3\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u516d\u3001\u4fdd\u5b58\u5e26\u6709\u4e0d\u540c\u5750\u6807\u7cfb\u7684\u56fe\u5f62<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u540c\u4e00\u4e2a\u56fe\u5f62\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u5750\u6807\u7cfb\u3002Matplotlib\u7684<code>twinx<\/code>\u548c<code>twiny<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56fe\u5f62\u548c\u5750\u6807\u7cfb<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e00\u6761\u66f2\u7ebf<\/strong><\/h2>\n<p>ax1.plot(x, y, &#39;g-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X data&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y1 data&#39;, color=&#39;g&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u5750\u6807\u7cfb<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, [i2 for i in y], &#39;b-&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y2 data&#39;, color=&#39;b&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;my_plot_with_two_axes.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\uff0c\u5e76\u4f7f\u7528\u4e0d\u540c\u7684\u5750\u6807\u7cfb\u7ed8\u5236\u4e86\u4e24\u6761\u66f2\u7ebf\u3002<strong>\u4f7f\u7528\u4e0d\u540c\u7684\u5750\u6807\u7cfb\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u5168\u9762\u5730\u7406\u89e3\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5c06Python\u7ed8\u5236\u7684\u56fe\u5f62\u4fdd\u5b58\u4e0b\u6765\u662f\u4e00\u4e2a\u591a\u6b65\u9aa4\u7684\u8fc7\u7a0b\uff0c\u6d89\u53ca\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u3001\u8bbe\u7f6e\u5206\u8fa8\u7387\u3001\u8c03\u6574\u56fe\u5f62\u5c3a\u5bf8\u3001\u6dfb\u52a0\u6ce8\u91ca\u548c\u56fe\u4f8b\u3001\u5904\u7406\u591a\u4e2a\u5b50\u56fe\u3001\u521b\u5efa\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u4ee5\u53ca\u4f7f\u7528\u4e0d\u540c\u7684\u5750\u6807\u7cfb\u7b49\u3002<strong>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u548c\u4fdd\u5b58\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\uff0c\u4ee5\u6ee1\u8db3\u5404\u79cd\u9700\u6c42<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\uff1f<\/strong><br \/>Python\u7684\u7ed8\u56fe\u5e93\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u5982PNG\u3001JPEG\u3001SVG\u7b49\u3002\u4f7f\u7528Matplotlib\u4fdd\u5b58\u56fe\u50cf\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7<code>savefig()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>plt.savefig(&#39;figure.png&#39;, format=&#39;png&#39;)<\/code>\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002\u60a8\u53ea\u9700\u5728\u6587\u4ef6\u540d\u4e2d\u6307\u5b9a\u6240\u9700\u7684\u6269\u5c55\u540d\uff0cMatplotlib\u4f1a\u81ea\u52a8\u8bc6\u522b\u5e76\u4fdd\u5b58\u4e3a\u76f8\u5e94\u683c\u5f0f\u3002<\/p>\n<p><strong>\u4fdd\u5b58\u56fe\u50cf\u65f6\u53ef\u4ee5\u8bbe\u7f6e\u54ea\u4e9b\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>savefig()<\/code>\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u53c2\u6570\u6765\u4f18\u5316\u8f93\u51fa\u56fe\u50cf\u7684\u8d28\u91cf\u548c\u5916\u89c2\u3002\u5e38\u89c1\u7684\u53c2\u6570\u5305\u62ec<code>dpi<\/code>\uff08\u6bcf\u82f1\u5bf8\u70b9\u6570\uff0c\u5f71\u54cd\u56fe\u50cf\u6e05\u6670\u5ea6\uff09\u3001<code>bbox_inches<\/code>\uff08\u8bbe\u7f6e\u56fe\u50cf\u8fb9\u754c\u6846\uff09\u548c<code>transparent<\/code>\uff08\u4f7f\u80cc\u666f\u900f\u660e\uff09\u3002\u4f8b\u5982\uff0c<code>plt.savefig(&#39;figure.png&#39;, dpi=300, bbox_inches=&#39;tight&#39;)<\/code>\u53ef\u4ee5\u751f\u6210\u9ad8\u5206\u8fa8\u7387\u7684\u56fe\u50cf\uff0c\u5e76\u53bb\u9664\u591a\u4f59\u7684\u767d\u8fb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u4fdd\u5b58\u7684\u56fe\u50cf\u4e0e\u663e\u793a\u7684\u56fe\u50cf\u4e00\u81f4\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u4fdd\u5b58\u7684\u56fe\u50cf\u4e0e\u60a8\u5728\u5c4f\u5e55\u4e0a\u770b\u5230\u7684\u56fe\u50cf\u4e00\u81f4\uff0c\u6700\u597d\u5728\u8c03\u7528<code>savefig()<\/code>\u4e4b\u524d\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u67e5\u770b\u56fe\u5f62\u3002\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u786e\u4fdd\u6ca1\u6709\u672a\u663e\u793a\u7684\u5143\u7d20\uff0c\u6bd4\u5982\u56fe\u4f8b\u6216\u6807\u9898\u88ab\u5207\u6389\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.tight_layout()<\/code>\u8c03\u6574\u56fe\u5f62\u5e03\u5c40\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u5728\u9884\u671f\u8303\u56f4\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5feb\u901f\u56de\u7b54\uff1a \u4f7f\u7528savefig\u51fd\u6570\u3001\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u683c\u5f0f\u3001\u8bbe\u7f6e\u5206\u8fa8\u7387\u3001\u8c03\u6574\u56fe\u5f62\u5c3a\u5bf8\u3002Python\u7684Matplot [&hellip;]","protected":false},"author":3,"featured_media":1126321,"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\/1126314"}],"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=1126314"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126314\/revisions"}],"predecessor-version":[{"id":1126324,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126314\/revisions\/1126324"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126321"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}