{"id":1166166,"date":"2025-01-15T15:29:02","date_gmt":"2025-01-15T07:29:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1166166.html"},"modified":"2025-01-15T15:29:04","modified_gmt":"2025-01-15T07:29:04","slug":"python%e4%b8%adfig%e5%9b%be%e7%89%87%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1166166.html","title":{"rendered":"python\u4e2dfig\u56fe\u7247\u5982\u4f55\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210326\/07872fb3-3da8-4c7c-b983-762c76f15869.webp\" alt=\"python\u4e2dfig\u56fe\u7247\u5982\u4f55\u4fdd\u5b58\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u5b58matplotlib\u751f\u6210\u7684fig\u56fe\u7247\u3002<strong>\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5\u3001\u6307\u5b9a\u6587\u4ef6\u683c\u5f0f\u3001\u8c03\u6574\u5206\u8fa8\u7387\u3001\u4fdd\u5b58\u77e2\u91cf\u56fe<\/strong>\u7b49\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u4f60\u7075\u6d3b\u5730\u4fdd\u5b58\u56fe\u7247\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u4fbf\u6377\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58fig\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>savefig<\/code>\u662fmatplotlib\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u751f\u6210\u7684fig\u5bf9\u8c61\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\u53ca\u683c\u5f0f\uff0c\u5982PNG\u3001JPG\u3001SVG\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u4f7f\u7528savefig\u65b9\u6cd5\u4fdd\u5b58\u56fe\u7247<\/strong><\/h2>\n<p>fig.savefig(&#39;my_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6307\u5b9a\u6587\u4ef6\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u4fdd\u5b58\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u540d\u7684\u6269\u5c55\u540d\u6765\u6307\u5b9a\u56fe\u7247\u683c\u5f0f\u3002\u5e38\u89c1\u7684\u683c\u5f0f\u6709PNG\u3001JPG\u3001SVG\u548cPDF\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aJPG\u683c\u5f0f<\/p>\n<p>fig.savefig(&#39;my_figure.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aSVG\u683c\u5f0f<\/strong><\/h2>\n<p>fig.savefig(&#39;my_figure.svg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aPDF\u683c\u5f0f<\/strong><\/h2>\n<p>fig.savefig(&#39;my_figure.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u6574\u5206\u8fa8\u7387<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4fdd\u5b58\u9ad8\u5206\u8fa8\u7387\u7684\u56fe\u7247\u3002\u53ef\u4ee5\u901a\u8fc7<code>dpi<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u56fe\u7247\u7684\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u9ad8\u5206\u8fa8\u7387\u56fe\u7247<\/p>\n<p>fig.savefig(&#39;high_res_figure.png&#39;, dpi=300)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u77e2\u91cf\u56fe<\/h3>\n<\/p>\n<p><p>\u77e2\u91cf\u56fe\u683c\u5f0f\u5982SVG\u548cPDF\uff0c\u53ef\u4ee5\u5728\u7f29\u653e\u65f6\u4fdd\u6301\u56fe\u50cf\u7684\u6e05\u6670\u5ea6\uff0c\u8fd9\u5bf9\u4e8e\u6253\u5370\u6216\u9700\u8981\u9ad8\u8d28\u91cf\u56fe\u50cf\u7684\u5e94\u7528\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3a\u77e2\u91cf\u56fe\u683c\u5f0f<\/p>\n<p>fig.savefig(&#39;vector_figure.svg&#39;)<\/p>\n<p>fig.savefig(&#39;vector_figure.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u900f\u660e\u80cc\u666f\u7684\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4fdd\u5b58\u900f\u660e\u80cc\u666f\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528<code>transparent<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u900f\u660e\u80cc\u666f\u7684\u56fe\u7247<\/p>\n<p>fig.savefig(&#39;transparent_figure.png&#39;, transparent=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u90e8\u5206\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u53ea\u60f3\u4fdd\u5b58\u56fe\u7247\u7684\u4e00\u90e8\u5206\u3002\u53ef\u4ee5\u4f7f\u7528<code>bbox_inches<\/code>\u53c2\u6570\u6765\u88c1\u526a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247<\/p>\n<p>fig.savefig(&#39;cropped_figure.png&#39;, bbox_inches=&#39;tight&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4fdd\u5b58\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u56fe\u8868\u5927\u5c0f\u6765\u4fdd\u5b58\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u8868\u5927\u5c0f<\/p>\n<p>fig.set_size_inches(8, 6)  # \u5bbd8\u82f1\u5bf8\uff0c\u9ad86\u82f1\u5bf8<\/p>\n<p>fig.savefig(&#39;resized_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4fdd\u5b58\u5e26\u6709\u6ce8\u91ca\u548c\u56fe\u4f8b\u7684\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u56fe\u7247\u4e4b\u524d\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u6ce8\u91ca\u548c\u56fe\u4f8b\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u89e3\u91ca\u56fe\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6ce8\u91ca\u548c\u56fe\u4f8b<\/p>\n<p>ax.annotate(&#39;Important Point&#39;, xy=(2, 4), xytext=(3, 4.5),<\/p>\n<p>            arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<p>ax.legend([&#39;Sample Data&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u7247<\/strong><\/h2>\n<p>fig.savefig(&#39;annotated_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u4e0d\u540c\u540e\u7aef\u4fdd\u5b58\u56fe\u7247<\/h3>\n<\/p>\n<p><p>matplotlib\u652f\u6301\u591a\u79cd\u540e\u7aef\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u540e\u7aef\u6765\u4fdd\u5b58\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib<\/p>\n<p>matplotlib.use(&#39;Agg&#39;)  # \u4f7f\u7528Agg\u540e\u7aef<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868\u5e76\u4fdd\u5b58<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<p>fig.savefig(&#39;agg_backend_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4fdd\u5b58\u52a8\u753b<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u751f\u6210\u4e86\u52a8\u753b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>FuncAnimation<\/code>\u548c<code>save<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.animation as animation<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>x = [1, 2, 3, 4]<\/p>\n<p>y = [1, 4, 2, 3]<\/p>\n<p>line, = ax.plot(x, y)<\/p>\n<p>def update(num, x, y, line):<\/p>\n<p>    line.set_data(x[:num], y[:num])<\/p>\n<p>    return line,<\/p>\n<p>ani = animation.FuncAnimation(fig, update, frames=len(x), fargs=[x, y, line])<\/p>\n<h2><strong>\u4fdd\u5b58\u52a8\u753b<\/strong><\/h2>\n<p>ani.save(&#39;animation.mp4&#39;, writer=&#39;ffmpeg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4fdd\u5b58\u56fe\u8868\u5230\u5185\u5b58<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u56fe\u8868\u4fdd\u5b58\u5230\u5185\u5b58\u800c\u4e0d\u662f\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>BytesIO<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u4fdd\u5b58\u5230\u5185\u5b58<\/strong><\/h2>\n<p>buf = io.BytesIO()<\/p>\n<p>fig.savefig(buf, format=&#39;png&#39;)<\/p>\n<p>buf.seek(0)<\/p>\n<h2><strong>\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>import PIL.Image<\/p>\n<p>image = PIL.Image.open(buf)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4fdd\u5b58\u56fe\u8868\u5230PDF\u62a5\u544a<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.backends.backend_pdf.PdfPages<\/code>\u6765\u5c06\u591a\u4e2a\u56fe\u8868\u4fdd\u5b58\u5230\u4e00\u4e2aPDF\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.backends.backend_pdf import PdfPages<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig1, ax1 = plt.subplots()<\/p>\n<p>ax1.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<p>fig2, ax2 = plt.subplots()<\/p>\n<p>ax2.plot([1, 2, 3, 4], [4, 3, 2, 1])<\/p>\n<h2><strong>\u4fdd\u5b58\u5230PDF\u6587\u4ef6<\/strong><\/h2>\n<p>with PdfPages(&#39;multipage_pdf.pdf&#39;) as pdf:<\/p>\n<p>    pdf.savefig(fig1)<\/p>\n<p>    pdf.savefig(fig2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4fdd\u5b58\u56fe\u8868\u5230SVG\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>SVG\u662f\u4e00\u79cd\u77e2\u91cf\u56fe\u683c\u5f0f\uff0c\u9002\u5408\u9700\u8981\u9ad8\u8d28\u91cf\u56fe\u50cf\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aSVG\u6587\u4ef6<\/p>\n<p>fig.savefig(&#39;figure.svg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4fdd\u5b58\u56fe\u8868\u5230EPS\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>EPS\u662f\u4e00\u79cd\u7528\u4e8e\u6253\u5370\u7684\u77e2\u91cf\u56fe\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aEPS\u6587\u4ef6<\/p>\n<p>fig.savefig(&#39;figure.eps&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4fdd\u5b58\u56fe\u8868\u5230JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u56fe\u8868\u6570\u636e\u4fdd\u5b58\u5230JSON\u6587\u4ef6\uff0c\u4ee5\u4fbf\u65e5\u540e\u52a0\u8f7d\u548c\u518d\u73b0\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4],<\/p>\n<p>    &#39;y&#39;: [1, 4, 2, 3]<\/p>\n<p>}<\/p>\n<h2><strong>\u4fdd\u5b58\u5230JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;figure.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(data, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4eceJSON\u6587\u4ef6\u52a0\u8f7d\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4eceJSON\u6587\u4ef6\u52a0\u8f7d\u56fe\u8868\u6570\u636e\uff0c\u5e76\u91cd\u65b0\u751f\u6210\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u4eceJSON\u6587\u4ef6\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;figure.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    data = json.load(f)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(data[&#39;x&#39;], data[&#39;y&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;loaded_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4fdd\u5b58\u56fe\u8868\u5230HTML\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u56fe\u8868\u5d4c\u5165\u5230HTML\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u5728\u7f51\u9875\u4e0a\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mpld3<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u4fdd\u5b58\u5230HTML\u6587\u4ef6<\/strong><\/h2>\n<p>html_str = mpld3.fig_to_html(fig)<\/p>\n<p>with open(&#39;figure.html&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(html_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u4fdd\u5b58\u56fe\u8868\u5230LaTeX\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u56fe\u8868\u4fdd\u5b58\u4e3aPGF\u6587\u4ef6\uff0c\u7136\u540e\u5728LaTeX\u6587\u6863\u4e2d\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u540e\u7aef\u4e3aPGF<\/p>\n<p>matplotlib.use(&#39;pgf&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aPGF\u6587\u4ef6<\/strong><\/h2>\n<p>fig.savefig(&#39;figure.pgf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u4f7f\u7528Seaborn\u4fdd\u5b58\u56fe\u8868<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8ematplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u4fdd\u5b58Seaborn\u751f\u6210\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>sns.scatterplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips, ax=ax)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;seaborn_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u4f7f\u7528Plotly\u4fdd\u5b58\u56fe\u8868<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plotly.io<\/code>\u6a21\u5757\u4fdd\u5b58\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>import plotly.io as pio<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\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\u56fe\u8868<\/strong><\/h2>\n<p>pio.write_image(fig, &#39;plotly_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e00\u3001\u4fdd\u5b583D\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>mpl_toolkits.mplot3d<\/code>\u6a21\u5757\u521b\u5efa\u548c\u4fdd\u5b583D\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mpl_toolkits.mplot3d import Axes3D<\/p>\n<h2><strong>\u521b\u5efa3D\u56fe\u8868<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<p>ax.scatter([1, 2, 3, 4], [1, 4, 2, 3], [10, 20, 15, 10])<\/p>\n<h2><strong>\u4fdd\u5b583D\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;3d_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e8c\u3001\u4fdd\u5b58\u6781\u5750\u6807\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>projection=&#39;polar&#39;<\/code>\u521b\u5efa\u548c\u4fdd\u5b58\u6781\u5750\u6807\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6781\u5750\u6807\u56fe\u8868<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;polar&#39;)<\/p>\n<p>ax.plot([0, 1, 2, 3], [1, 4, 2, 3])<\/p>\n<h2><strong>\u4fdd\u5b58\u6781\u5750\u6807\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;polar_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e09\u3001\u4fdd\u5b58\u70ed\u56fe<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>imshow<\/code>\u521b\u5efa\u548c\u4fdd\u5b58\u70ed\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u70ed\u56fe<\/strong><\/h2>\n<p>data = np.random.random((10, 10))<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>cax = ax.imshow(data, cmap=&#39;hot&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u70ed\u56fe<\/strong><\/h2>\n<p>fig.savefig(&#39;heatmap.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u56db\u3001\u4fdd\u5b58\u5e26\u6709\u989c\u8272\u6761\u7684\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6dfb\u52a0\u989c\u8272\u6761\u5e76\u4fdd\u5b58\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u70ed\u56fe<\/p>\n<p>data = np.random.random((10, 10))<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>cax = ax.imshow(data, cmap=&#39;hot&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u989c\u8272\u6761<\/strong><\/h2>\n<p>fig.colorbar(cax)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;heatmap_with_colorbar.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e94\u3001\u4fdd\u5b58\u5b50\u56fe<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\u5e76\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5b50\u56fe<\/p>\n<p>fig, axs = plt.subplots(2, 2)<\/p>\n<p>axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<p>axs[0, 1].plot([1, 2, 3, 4], [4, 3, 2, 1])<\/p>\n<p>axs[1, 0].plot([1, 2, 3, 4], [2, 3, 2, 3])<\/p>\n<p>axs[1, 1].plot([1, 2, 3, 4], [3, 2, 3, 2])<\/p>\n<h2><strong>\u4fdd\u5b58\u5b50\u56fe<\/strong><\/h2>\n<p>fig.savefig(&#39;subplots.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u516d\u3001\u4fdd\u5b58\u5e26\u6709\u6807\u9898\u548c\u6807\u7b7e\u7684\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\u5e76\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56fe\u8868<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&#39;Sample Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;X Axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;labeled_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e03\u3001\u4fdd\u5b58\u5e26\u6709\u7f51\u683c\u7684\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\u5e76\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56fe\u8868<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u7f51\u683c<\/strong><\/h2>\n<p>ax.grid(True)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/h2>\n<p>fig.savefig(&#39;grid_figure.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u4fdd\u5b58matplotlib\u751f\u6210\u7684fig\u56fe\u7247\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u65e0\u8bba\u662f\u4fdd\u5b58\u9759\u6001\u56fe\u8868\u3001\u52a8\u6001\u52a8\u753b\uff0c\u8fd8\u662f\u9ad8\u5206\u8fa8\u7387\u56fe\u7247\u3001\u77e2\u91cf\u56fe\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u9002\u5f53\u7684\u53c2\u6570\u8bbe\u7f6e\u548c\u65b9\u6cd5\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4fdd\u5b58\u548c\u5206\u4eab\u4f60\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\u901a\u5e38\u4f7f\u7528Matplotlib\u5e93\uff0c\u901a\u8fc7<code>savefig()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u60a8\u53ea\u9700\u63d0\u4f9b\u6587\u4ef6\u540d\u548c\u6240\u9700\u683c\u5f0f\uff08\u5982PNG\u3001JPEG\u7b49\uff09\uff0c\u4f8b\u5982\uff1a<code>plt.savefig(&#39;my_plot.png&#39;)<\/code>\uff0c\u8fd9\u5c06\u628a\u5f53\u524d\u56fe\u5f62\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8c03\u7528\u6b64\u51fd\u6570\u4e4b\u524d\u5df2\u7ecf\u521b\u5efa\u4e86\u56fe\u5f62\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4fdd\u5b58\u54ea\u4e9b\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>Matplotlib\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\uff0c\u5305\u62ecPNG\u3001JPEG\u3001PDF\u3001SVG\u7b49\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u9700\u8981\u9ad8\u8d28\u91cf\u8f93\u51fa\u65f6\u53ef\u4ee5\u9009\u62e9PDF\u683c\u5f0f\uff0c\u800c\u9700\u8981\u5728\u7f51\u9875\u4e0a\u4f7f\u7528\u65f6\u53ef\u4ee5\u9009\u62e9PNG\u6216SVG\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u4fdd\u5b58\u56fe\u50cf\u7684\u5206\u8fa8\u7387\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>savefig()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dpi<\/code>\u53c2\u6570\u8bbe\u7f6e\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u3002\u4f8b\u5982\uff0c<code>plt.savefig(&#39;my_plot.png&#39;, dpi=300)<\/code>\u4f1a\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a300 DPI\u7684\u9ad8\u5206\u8fa8\u7387\u6587\u4ef6\u3002\u8c03\u6574DPI\u503c\u53ef\u4ee5\u6539\u5584\u56fe\u50cf\u7684\u6e05\u6670\u5ea6\uff0c\u7279\u522b\u662f\u5728\u6253\u5370\u65f6\u975e\u5e38\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u5b58matplotlib\u751f\u6210\u7684fig\u56fe\u7247\u3002\u4f7f\u7528savefig\u65b9\u6cd5\u3001\u6307\u5b9a\u6587\u4ef6\u683c\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1166173,"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\/1166166"}],"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=1166166"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166166\/revisions"}],"predecessor-version":[{"id":1166175,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166166\/revisions\/1166175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1166173"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1166166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1166166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1166166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}