{"id":2096,"date":"2023-04-01T22:06:38","date_gmt":"2023-04-01T14:06:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2096"},"modified":"2023-04-06T13:57:39","modified_gmt":"2023-04-06T05:57:39","slug":"matplotlib-plt-subplots","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/matplotlib-plt-subplots\/","title":{"rendered":"matplotlib\u4e4bplt.subplots"},"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.subplots.html\">https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.subplots.html<\/a><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\u5bf9\u8c61\uff08figure\uff09\u548c\u4e00\u7cfb\u5217\u7684\u5b50\u56fe\uff08subplots\uff09\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,\n             subplot_kw=None, gridspec_kw=None, **fig_kw):\n    fig = figure(**fig_kw)\n    axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,\n                       squeeze=squeeze, subplot_kw=subplot_kw,\n                       gridspec_kw=gridspec_kw)\n    return fig, axs<\/code><\/pre>\n<p>\u53c2\u6570<\/p>\n<p>\uff081\uff09nrows, ncols<\/p>\n<p>\u5b50\u56fe\u7684\u884c\u5217\u6570\u3002<\/p>\n<p>\uff082\uff09sharex, sharey<\/p>\n<p>\u8bbe\u7f6e\u4e3a True \u6216\u8005 &#8216;all&#8217; \u65f6\uff0c\u6240\u6709\u5b50\u56fe\u5171\u4eab x \u8f74\u6216\u8005 y \u8f74\uff0c<br \/>\n\u8bbe\u7f6e\u4e3a False or &#8216;none&#8217; \u65f6\uff0c\u6240\u6709\u5b50\u56fe\u7684 x\uff0cy \u8f74\u5747\u4e3a\u72ec\u7acb\uff0c<br \/>\n\u8bbe\u7f6e\u4e3a &#8216;row&#8217; \u65f6\uff0c\u6bcf\u4e00\u884c\u7684\u5b50\u56fe\u4f1a\u5171\u4eab x \u6216\u8005 y \u8f74\uff0c<br \/>\n\u8bbe\u7f6e\u4e3a &#8216;col&#8217; \u65f6\uff0c\u6bcf\u4e00\u5217\u7684\u5b50\u56fe\u4f1a\u5171\u4eab x \u6216\u8005 y \u8f74\u3002<\/p>\n<p>\uff083\uff09squeeze<\/p>\n<p>\u9ed8\u8ba4\u4e3a True\uff0c\u662f\u8bbe\u7f6e\u8fd4\u56de\u7684\u5b50\u56fe\u5bf9\u8c61\u7684\u6570\u7ec4\u683c\u5f0f\u3002<br \/>\n\u5f53\u4e3a False \u65f6\uff0c\u4e0d\u8bba\u8fd4\u56de\u7684\u5b50\u56fe\u662f\u53ea\u6709\u4e00\u4e2a\u8fd8\u662f\u53ea\u6709\u4e00\u884c\uff0c\u90fd\u4f1a\u7528\u4e8c\u7ef4\u6570\u7ec4\u683c\u5f0f\u8fd4\u56de\u5176\u5bf9\u8c61\u3002<br \/>\n\u5f53\u4e3a True \u65f6\uff0c\u5982\u679c\u8bbe\u7f6e\u7684\u5b50\u56fe\u662f\uff08nrows=ncols=1\uff09\uff0c\u5373\u5b50\u56fe\u53ea\u6709\u4e00\u4e2a\uff0c\u5219\u8fd4\u56de\u7684\u5b50\u56fe\u5bf9\u8c61\u662f\u4e00\u4e2a\u6807\u91cf\u7684\u5f62\u5f0f\uff0c\u5982\u679c\u5b50\u56fe\u6709\uff08N\u00d71\uff09\u6216\u8005\uff081\u00d7N\uff09\u4e2a\uff0c\u5219\u8fd4\u56de\u7684\u5b50\u56fe\u5bf9\u8c61\u662f\u4e00\u7ef4\u6570\u7ec4\u7684\u683c\u5f0f\uff0c\u5982\u679c\u662f\uff08N\u00d7M\uff09\u5219\u662f\u8fd4\u56de\u4e8c\u7ef4\u683c\u5f0f\u3002<\/p>\n<p>\uff084\uff09subplot_kw<\/p>\n<p>\u5b57\u5178\u683c\u5f0f\uff0c\u4f20\u9012\u7ed9 add_subplot()\uff0c\u7528\u4e8e\u521b\u5efa\u5b50\u56fe\u3002<\/p>\n<p>\uff085\uff09gridspec_kw<\/p>\n<p>\u5b57\u5178\u683c\u5f0f\uff0c\u4f20\u9012\u7ed9 GridSpec \u7684\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5b50\u56fe\u6240\u6446\u653e\u7684\u7f51\u683c\u3002<\/p>\n<pre><code>class matplotlib.gridspec.GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)<\/code><\/pre>\n<p>\u5982\uff0c\u8bbe\u7f6e gridspec_kw={&#8216;height_ratios&#8217;: [3, 1]} \u5219\u5b50\u56fe\u5728\u5217\u4e0a\u7684\u5206\u5e03\u6bd4\u4f8b\u662f3\u6bd41\u3002<\/p>\n<p>\uff086\uff09**fig_kw<\/p>\n<p>\u6240\u6709\u5176\u4ed6\u5173\u952e\u5b57\u53c2\u6570\u90fd\u4f20\u9012\u7ed9 figure() \u8c03\u7528\u3002\u5982\uff0c\u8bbe\u7f6e figsize=(21, 12) \uff0c\u5219\u8bbe\u7f6e\u4e86\u56fe\u50cf\u5927\u5c0f\u3002<\/p>\n<p>\u8fd4\u56de\u503c<\/p>\n<p>\uff081\uff09fig\uff1amatplotlib.figure.Figure \u5bf9\u8c61<br \/>\n\uff082\uff09ax\uff1a\u5b50\u56fe\u5bf9\u8c61\uff08 matplotlib.axes.Axes\uff09\u6216\u8005\u662f\u4ed6\u7684\u6570\u7ec4<\/p>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 2 * np.pi, 400)\ny = np.sin(x ** 2)\n\n# \u521b\u5efa\u4e00\u4e2a\u5b50\u56fe\nfig, ax = plt.subplots()\nax.plot(x, y)\nax.set_title(&#039;Simple subplots&#039;)\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_basic.png\" alt=\"matplotlib_subplots_basic\" \/><\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 2 * np.pi, 400)\ny = np.sin(x ** 2)\n\n# \u521b\u5efa\u4e24\u4e2a\u5b50\u56fe\uff0c\u5e76\u4e14\u5171\u4eaby\u8f74\nf, (ax1, ax2) = plt.subplots(1, 2, sharey=True)\nax1.plot(x, y)\nax1.set_title(&#039;Sharing Y axis&#039;)\nax2.scatter(x, y)\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_share.png\" alt=\"matplotlib_subplots_share\" \/><\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 2 * np.pi, 400)\ny = np.sin(x ** 2)\n\n# \u521b\u5efa4\u4e2a\u5b50\u56fe\uff0c\nfig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))  # polar: \u6781\u5750\u6807\u8f74\naxes[0, 0].plot(x, y)\naxes[1, 1].scatter(x, y)\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_polar.png\" alt=\"matplotlib_subplots_square\" \/><\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u5171\u4eab\u6bcf\u5217\u5b50\u56fe\u7684x\u8f74\n# plt.subplots(2, 2, sharex=&#039;col&#039;)\n# \u5171\u4eab\u6bcf\u884c\u5b50\u56fe\u7684y\u8f74\n# plt.subplots(2, 2, sharey=&#039;row&#039;)\n# \u5171\u4eab\u6240\u6709\u5b50\u56fe\u7684x\u548cy\u8f74\n# plt.subplots(2, 2, sharex=&#039;all&#039;, sharey=&#039;all&#039;)\nplt.subplots(2, 2, sharex=True, sharey=True)\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_shareX.png\" alt=\"matplotlib_subplots_shareX\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_shareY.png\" alt=\"matplotlib_subplots_shareY\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplots_shareXY.png\" alt=\"matplotlib_subplots_shareXY\" \/><\/p>\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-2096","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\/2096","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=2096"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2096\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}