{"id":2095,"date":"2023-04-01T22:05:56","date_gmt":"2023-04-01T14:05:56","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2095"},"modified":"2023-04-06T13:57:48","modified_gmt":"2023-04-06T05:57:48","slug":"matplotlib-plt-subplot","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/matplotlib-plt-subplot\/","title":{"rendered":"matplotlib\u4e4bplt.subplot"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>matplotlib.pyplot.subplot\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.subplot.html\">https:\/\/matplotlib.org\/api\/_as_gen\/matplotlib.pyplot.subplot.html<\/a><\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u5b50\u56fe\u5206\u5e03\u7684\u7f51\u683c\u4f4d\u7f6e\u7684 figure \u5bf9\u8c61\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">subplot(nrows, ncols, index, **kwargs)\nsubplot(pos, **kwargs)\nsubplot(ax)<\/code><\/pre>\n<p>\u5206\u522b\u6307\u5b9a(\u884c\u6570\u3001\u5217\u6570\u3001\u4f4d\u7f6e)<\/p>\n<p>\u5176\u4ed6\u53c2\u6570<\/p>\n<ul>\n<li>facecolor: \u80cc\u666f\u8272<\/li>\n<li>polar: \u662f\u5426\u7528\u6781\u5750\u6807\u6295\u5f71<\/li>\n<li>projection: \u6295\u5f71<\/li>\n<\/ul>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# plot a line, implicitly creating a subplot(111)\nplt.plot([1, 2, 3])\nplt.show()\n\n# now create a subplot which represents the top plot of a grid\n# with 2 rows and 1 column. Since this subplot will overlap the\n# first, the plot (and its axes) previously created, will be removed\nplt.subplot(211)\nplt.plot(range(12))\nplt.subplot(212, facecolor=&#039;y&#039;)  # creates 2nd subplot with yellow background\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplot_basic1.png\" alt=\"matplotlib_subplot_basic\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplot_basic2.png\" alt=\"matplotlib_subplot_basic\" \/><\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n&#039;&#039;&#039;\nnumpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n\u5728\u6307\u5b9a\u7684\u95f4\u9694\u5185\u8fd4\u56de\u5747\u5300\u95f4\u9694\u7684\u6570\u5b57\uff0c\u8fd4\u56denum\u5747\u5300\u5206\u5e03\u7684\u6837\u672c\u5728[start, stop]\u4e4b\u95f4\n&#039;&#039;&#039;\nx1 = np.linspace(0.0, 5.0)\nx2 = np.linspace(0.0, 2.0)\ny1 = np.cos(2 * np.pi * x1) * np.exp(-x1)\ny2 = np.cos(2 * np.pi * x2)\n\nplt.subplot(2, 1, 1)\nplt.plot(x1, y1, &#039;o-&#039;)\nplt.title(&#039;A tale of 2 subplots&#039;)\nplt.ylabel(&#039;Damped oscillation&#039;)\n\nplt.subplot(2, 1, 2)\nplt.plot(x2, y2, &#039;.-&#039;)\nplt.xlabel(&#039;time (s)&#039;)\nplt.ylabel(&#039;Undamped&#039;)\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplot_numpy.png\" alt=\"matplotlib_subplot_numpy\" \/><\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nsquare = np.zeros((32, 32))\n\nsquare[5:10, 5:10] = 1\nplt.subplot(221)\nplt.imshow(square)\n\nsquare[15:20, 5:10] = 1\nplt.subplot(222)\nplt.imshow(square)\n\nsquare[5:10, 15:20] = 1\nplt.subplot(223)\nplt.imshow(square)\n\nsquare[15:20, 15:20] = 1\nplt.subplot(224)\nplt.imshow(square)\n\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/AI&amp;ML\/matplotlib_subplot_square.png\" alt=\"matplotlib_subplot_square\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb matplotlib.pyplot.subplot\uff1ahttps:\/\/matplotlib.org\/api [&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-2095","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\/2095","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=2095"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2095\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}