{"id":1097786,"date":"2025-01-08T15:15:09","date_gmt":"2025-01-08T07:15:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097786.html"},"modified":"2025-01-08T15:15:11","modified_gmt":"2025-01-08T07:15:11","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%ba%e7%94%bb%e7%9a%84%e5%9b%be%e7%89%87%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097786.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u51fa\u753b\u7684\u56fe\u7247\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212414\/5cc881b8-7ea3-43c0-85a5-07b460547275.webp\" alt=\"python\u5982\u4f55\u5bfc\u51fa\u753b\u7684\u56fe\u7247\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u5bfc\u51fa\u56fe\u7247\u7684\u683c\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ecPNG\u3001JPEG\u3001SVG\u7b49\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\uff0c\u5982Matplotlib\u3001Pillow\u3001Seaborn\u7b49\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u6765\u5bfc\u51fa\u56fe\u7247\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>Matplotlib\u5bfc\u51fa\u56fe\u7247<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5730\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5e76\u4e14\u652f\u6301\u5c06\u8fd9\u4e9b\u56fe\u5f62\u5bfc\u51fa\u4e3a\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Matplotlib\u5bfc\u51fa\u56fe\u7247<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u4fdd\u5b58\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u521b\u5efa\u5e76\u4fdd\u5b58\u4e00\u4e2a\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\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>\u521b\u5efa\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;figure.png&#39;)  # \u8fd9\u91cc\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5982&#39;figure.jpg&#39;, &#39;figure.svg&#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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>plt.savefig(&#39;figure.png&#39;)<\/code>\u7528\u4e8e\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u7684\u56fe\u7247\u3002\u4f60\u53ef\u4ee5\u5c06\u6587\u4ef6\u6269\u5c55\u540d\u66f4\u6539\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u5982JPEG\u6216SVG\uff0c\u4ee5\u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8c03\u6574\u56fe\u7247\u8d28\u91cf\u548c\u5206\u8fa8\u7387<\/h4>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u8c03\u6574\u56fe\u7247\u7684\u8d28\u91cf\u548c\u5206\u8fa8\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dpi<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u56fe\u7247\u7684\u5206\u8fa8\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;figure.png&#39;, dpi=300)  # \u8bbe\u7f6e\u5206\u8fa8\u7387\u4e3a300 DPI<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4fdd\u5b58\u4e3a\u900f\u660e\u80cc\u666f\u7684\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4fdd\u5b58\u4e00\u4e2a\u900f\u660e\u80cc\u666f\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528<code>transparent<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;figure.png&#39;, transparent=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Pillow\u5bfc\u51fa\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u5904\u7406\u64cd\u4f5c\uff0c\u5305\u62ec\u521b\u5efa\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u4fdd\u5b58\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (200, 200), &#39;white&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e9b\u56fe\u5f62<\/strong><\/h2>\n<p>draw.line((0, 0, 200, 200), fill=&#39;black&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;image.png&#39;)  # \u8fd9\u91cc\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5982&#39;image.jpg&#39;, &#39;image.bmp&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Seaborn\u5bfc\u51fa\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u66f4\u52a0\u7f8e\u89c2\u548c\u590d\u6742\u7684\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Seaborn<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5e76\u4fdd\u5b58\u56fe\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>sns.violinplot(x=&#39;day&#39;, y=&#39;total_bill&#39;, data=tips)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;seaborn_figure.png&#39;)  # \u8fd9\u91cc\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5982&#39;seaborn_figure.jpg&#39;<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u5e93\u5bfc\u51fa\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u3001Pillow\u548cSeaborn\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5bfc\u51fa\u56fe\u7247\uff0c\u4f8b\u5982Plotly\u3001Bokeh\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Plotly<\/h4>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u56fe\u5f62\u5e93\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u5bfc\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>fig = px.scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13])<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>fig.write_image(&#39;plotly_figure.png&#39;)  # \u8fd9\u91cc\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5982&#39;plotly_figure.jpeg&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Bokeh<\/h4>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, output_file, save<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62<\/strong><\/h2>\n<p>p = figure(title=&quot;Simple Line Example&quot;, x_axis_label=&#39;x&#39;, y_axis_label=&#39;y&#39;)<\/p>\n<p>p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], legend_label=&quot;Temp.&quot;, line_width=2)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>output_file(&quot;bokeh_plot.html&quot;)<\/p>\n<p>save(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5bfc\u51fa\u56fe\u7247\uff0c\u5305\u62ec<strong>Matplotlib\u3001Pillow\u3001Seaborn\u3001Plotly\u548cBokeh<\/strong>\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u521b\u5efa\u548c\u5bfc\u51fa\u56fe\u7247\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u9759\u6001\u56fe\u5f62\u8fd8\u662f\u590d\u6742\u7684\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0cPython\u90fd\u80fd\u591f\u8f7b\u677e\u5730\u5b9e\u73b0\u5e76\u5bfc\u51fa\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u7247\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u51fa\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u5bfc\u51fa\u56fe\u50cf\u5230\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ecPNG\u3001JPEG\u3001SVG\u548cPDF\u7b49\u3002\u901a\u8fc7\u8c03\u7528<code>savefig()<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u53c2\u6570\u4e2d\u6307\u5b9a\u6240\u9700\u7684\u6587\u4ef6\u540d\u548c\u683c\u5f0f\u5373\u53ef\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>plt.savefig(&#39;my_image.png&#39;)<\/code>\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5bfc\u51fa\u56fe\u7247\u65f6\u5982\u4f55\u9009\u62e9\u5408\u9002\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\u8f83\u9ad8\u7684DPI\uff08\u6bcf\u82f1\u5bf8\u70b9\u6570\uff09\u503c\u5c06\u751f\u6210\u66f4\u6e05\u6670\u7684\u56fe\u50cf\u3002\u4f8b\u5982\uff0c<code>plt.savefig(&#39;my_image.png&#39;, dpi=300)<\/code>\u5c06\u4ee5300DPI\u7684\u5206\u8fa8\u7387\u4fdd\u5b58\u56fe\u50cf\uff0c\u9002\u5408\u6253\u5370\u7528\u9014\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5bfc\u51fa\u7684\u56fe\u50cf\u4fdd\u6301\u539f\u59cb\u56fe\u5f62\u7684\u6bd4\u4f8b\u548c\u6e05\u6670\u5ea6\uff1f<\/strong><br \/>\u5728\u5bfc\u51fa\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bbox_inches=&#39;tight&#39;<\/code>\u53c2\u6570\u6765\u81ea\u52a8\u8c03\u6574\u56fe\u50cf\u7684\u8fb9\u754c\u6846\uff0c\u4ece\u800c\u786e\u4fdd\u56fe\u50cf\u7684\u5185\u5bb9\u4e0d\u4f1a\u88ab\u88c1\u526a\uff0c\u540c\u65f6\u4fdd\u6301\u539f\u59cb\u6bd4\u4f8b\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a<code>plt.savefig(&#39;my_image.png&#39;, bbox_inches=&#39;tight&#39;)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5bfc\u51fa\u7684\u56fe\u50cf\u5c3d\u53ef\u80fd\u5b8c\u6574\u4e14\u6bd4\u4f8b\u6b63\u786e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bfc\u51fa\u56fe\u7247\u7684\u683c\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ecPNG\u3001JPEG\u3001SVG\u7b49\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\uff0c\u5982Matplot [&hellip;]","protected":false},"author":3,"featured_media":1097794,"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\/1097786"}],"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=1097786"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097786\/revisions"}],"predecessor-version":[{"id":1097795,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097786\/revisions\/1097795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097794"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}