{"id":2097,"date":"2023-04-01T22:07:18","date_gmt":"2023-04-01T14:07:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2097"},"modified":"2023-04-06T13:57:32","modified_gmt":"2023-04-06T05:57:32","slug":"matplotlib-plt-figure","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/matplotlib-plt-figure\/","title":{"rendered":"matplotlib\u4e4bplt.figure"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>matplotlib.pyplot.subplots\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.figure.html\">https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.figure.html<\/a><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\uff08figure\uff09\u5bf9\u8c61\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">def figure(num=None, \u00a0# autoincrement if None, else integer from 1-N\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0figsize=None, \u00a0# defaults to rc figure.figsize\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0dpi=None, \u00a0# defaults to rc figure.dpi\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0facecolor=None, \u00a0# defaults to rc figure.facecolor\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0edgecolor=None, \u00a0# defaults to rc figure.edgecolor\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0frameon=True,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0FigureClass=Figure,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0clear=False,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0**kwargs\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0);<\/code><\/pre>\n<p>\u53c2\u6570<\/p>\n<p>\uff081\uff09num: \u56fe\u50cf\u7684\u7f16\u53f7(\u540d\u79f0)\uff0c\u53c2\u6570\u6ca1\u6709\u63d0\u4f9b\u65f6\uff0c\u4f1a\u81ea\u52a8\u7684\u7ef4\u6301\u4e00\u4e2a\u81ea\u589e\u7684\u6570\u5b57\u540d\u79f0\u3002\u5f53\u63d0\u4f9b\u4e86\u53c2\u6570\u65f6\uff0c\u5982\u679c\u6b64figure\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6b64\u5bf9\u8c61\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u5e76\u8fd4\u56defigure<\/p>\n<p>\uff082\uff09figsize: \u6574\u6570\u578b\u7684\u5143\u7ec4\u683c\u5f0f\uff0c\u9ed8\u8ba4\u4e3a None\uff0c\u4ee5\u5143\u7ec4\u5f62\u5f0f\uff08\u5bbd\uff0c\u9ad8\uff09\u63d0\u4f9b\u5bbd\u9ad8\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u4e3a\u82f1\u5c3a\uff0c\u82e5\u6ca1\u6709\u63d0\u4f9b\uff0c\u5219\u5927\u5c0f\u4e3a rc figure.figsize \u5b9a\u4e49\u7684\u503c<\/p>\n<p>\uff083\uff09dpi: \u6574\u6570\uff0c\u9ed8\u8ba4\u4e3a None\uff08\u9ed8\u8ba4\u503c rc figure.dpi.\uff09\uff0c\u8bbe\u7f6e figure \u7684\u5206\u8fa8\u7387<\/p>\n<p>\uff084\uff09facecolor: \u80cc\u666f\u989c\u8272\uff0c\u9ed8\u8ba4\u4e3a rc figure.facecolor. \u7684\u503c<\/p>\n<p>\uff085\uff09edgecolor: \u8fb9\u6846\u989c\u8272\uff0c\u9ed8\u8ba4\u4e3a rc figure.edgecolor. \u7684\u503c<\/p>\n<p>\uff086\uff09frameon: \u662f\u5426\u663e\u793a\u7ed8\u5236\u56fe\u6846\u3002<\/p>\n<p>\uff087\uff09FigureClass: \u4ecematplotlib.figure.Figure\u6d3e\u751f\u7684\u7c7b\uff0c\u53ef\u7528\u4e8e\u81ea\u5b9a\u4e49Figure\u5b9e\u4f8b<\/p>\n<p>\uff088\uff09clear\uff1abool\uff0c\u53ef\u9009\uff0c\u9ed8\u8ba4\u4e3aFalse\u3002\u5982\u679c\u4e3aTrue\u5e76\u4e14\u5df2\u7ecf\u5b58\u5728figure\uff0c\u90a3\u4e48\u5b83\u5c06\u88ab\u6e05\u9664<\/p>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(1, 100, 100)\ny1 = np.random.randint(20, 60, size=100)\ny2 = np.random.randint(30, 70, size=100)\ny3 = np.random.randint(50, 90, size=100)\n\nplt.figure(num=&quot;111&quot;, figsize=(6, 4), facecolor=&quot;pink&quot;, edgecolor=&quot;green&quot;)\nplt.plot(x, y1, c=&quot;red&quot;, label=&quot;y1_low&quot;)\nplt.plot(x, y2, c=&quot;blue&quot;, label=&quot;y2_middle&quot;)\nplt.plot(x, y3, c=&quot;yellow&quot;, label=&quot;y3_high&quot;)\nplt.legend(loc=&quot;best&quot;)\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_figure.png\" alt=\"matplotlib_figure\" \/><\/p>\n<h2>figure\u7ef4\u62a4<\/h2>\n<p>figure \u7684\u91cd\u590d\u5229\u7528\u80fd\u5927\u5927\u8282\u7ea6\u65f6\u95f4\uff0c\u4f46\u662f matplotlib \u7ef4\u62a4\u7684 figure \u6709\u6570\u91cf\u4e0a\u9650\uff08RuntimeWarning: More than 20 figures have been opened.\uff09\u3002\u5e76\u4e14\uff0c\u4e0d\u65ad\u7684\u521b\u5efa\u65b0\u7684 figure \u5b9e\u4f8b\uff0c\u5f88\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\uff0c\u800c\u5e94\u5408\u7406\u7684\u590d\u7528\uff0c\u80fd\u5927\u5927\u7684\u63d0\u9ad8\u8fd0\u884c\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e0d\u6e05\u7406 figure \u5c06\u6709\u53ef\u80fd\u9020\u6210\u5728\u7b2c\u4e00\u5e45\u4e2d plot \u7684\u7ebf\u518d\u6b21\u51fa\u73b0\u5728\u7b2c\u4e8c\u5e45\u56fe\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>plt.cla()<\/code>  # \u6e05\u9664axes\uff0c\u5373\u5f53\u524d figure \u4e2d\u7684\u6d3b\u52a8\u7684axes\uff0c\u4f46\u5176\u4ed6axes\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<li><code>plt.clf()<\/code>  # \u6e05\u9664\u5f53\u524d figure \u7684\u6240\u6709axes\uff0c\u4f46\u662f\u4e0d\u5173\u95ed\u8fd9\u4e2a window\uff0c\u6240\u4ee5\u80fd\u7ee7\u7eed\u590d\u7528\u4e8e\u5176\u4ed6\u7684 plot\u3002<\/li>\n<li><code>plt.close()<\/code>  # \u5173\u95ed window\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u6307\u5f53\u524d window\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-python\">Close a figure window.\n\n``close()`` by itself closes the current figure\n\n``close(fig)`` closes the `~.Figure` instance *fig*\n\n``close(num)`` closes the figure number *num*\n\n``close(name)`` where *name* is a string, closes figure with that label\n\n``close(&#039;all&#039;)`` closes all the figure windows<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb matplotlib.pyplot.subplots\uff1ahttps:\/\/matplotlib.org\/ap [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[527],"tags":[529],"class_list":["post-2097","post","type-post","status-publish","format-standard","hentry","category-ai","tag-matplotlib"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2097\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}