{"id":228,"date":"2022-08-19T20:45:20","date_gmt":"2022-08-19T12:45:20","guid":{"rendered":"http:\/\/162.14.82.114\/?p=228"},"modified":"2022-08-19T20:49:21","modified_gmt":"2022-08-19T12:49:21","slug":"matplotlib%e5%ad%a6%e4%b9%a0%e8%ae%b0%e5%bd%95%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/162.14.82.114\/index.php\/228\/08\/19\/2022\/","title":{"rendered":"Matplotlib\u5b66\u4e60\u8bb0\u5f55\uff08\u4e00\uff09"},"content":{"rendered":"<h1>Matplotlib\u521d\u76f8\u8bc6<\/h1>\n<h2>\u57fa\u7840<\/h2>\n<h3>\u5bfc\u5165\u76f8\u5173\u5e93\u51fd\u6570<\/h3>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib as mpl\nimport numpy as np<\/code><\/pre>\n<h3>\u753b\u51fa\u6700\u7b80\u5355\u7684\u56fe<\/h3>\n<pre><code>ax = plt.subplot()\nax.plot([1,2,3,4],[1,3,7,6])\nax<\/code><\/pre>\n<div align=center>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191950082.svg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191950082.svg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"temp1\" \/><\/div>\n<\/div>\n<pre><code class=\"language-python\">image = plt.plot([1, 2, 3, 4], [1, 4, 2, 3])<\/code><\/pre>\n<div align=center>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191953907.svg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208191953907.svg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"temp1\" \/><\/div>\n<\/div>\n<h2>Figure\u7684\u7ec4\u6210<\/h2>\n<ul>\n<li><code>Figure<\/code>\uff1a\u9876\u5c42\u7ea7\uff0c\u7528\u6765\u5bb9\u7eb3\u6240\u6709\u7ed8\u56fe\u5143\u7d20<\/li>\n<li><code>Axes<\/code>\uff1amatplotlib\u5b87\u5b99\u7684\u6838\u5fc3\uff0c\u5bb9\u7eb3\u4e86\u5927\u91cf\u5143\u7d20\u7528\u6765\u6784\u9020\u4e00\u5e45\u5e45\u5b50\u56fe\uff0c\u4e00\u4e2afigure\u53ef\u4ee5\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u5b50\u56fe\u7ec4\u6210<\/li>\n<li><code>Axis<\/code>\uff1aaxes\u7684\u4e0b\u5c5e\u5c42\u7ea7\uff0c\u7528\u4e8e\u5904\u7406\u6240\u6709\u548c\u5750\u6807\u8f74\uff0c\u7f51\u683c\u6709\u5173\u7684\u5143\u7d20<\/li>\n<li><code>Tick<\/code>\uff1aaxis\u7684\u4e0b\u5c5e\u5c42\u7ea7\uff0c\u7528\u6765\u5904\u7406\u6240\u6709\u548c\u523b\u5ea6\u6709\u5173\u7684\u5143\u7d20<\/li>\n<\/ul>\n<div align = \"center\">\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192020894.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192020894.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"img\" style=\"zoom: 50%;\" \/><\/div>\n<\/div>\n<h2>\u4e24\u79cd\u7ed8\u56fe\u63a5\u53e3<\/h2>\n<ul>\n<li>\n<p>\u663e\u5f0f\u521b\u5efafigure\u548caxes\uff0c\u5728\u4e0a\u9762\u8c03\u7528\u7ed8\u56fe\u65b9\u6cd5\uff0c\u4e5f\u88ab\u79f0\u4e3aOO\u6a21\u5f0f\uff08object-oriented style)<\/p>\n<\/li>\n<li>\n<p>\u4f9d\u8d56pyplot\u81ea\u52a8\u521b\u5efafigure\u548caxes\uff0c\u5e76\u7ed8\u56fe<\/p>\n<\/li>\n<\/ul>\n<h3>\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a<\/h3>\n<pre><code class=\"language-python\">x = np.linspace(0, 2, 100)\nfig, ax = plt.subplots()  \nax.plot(x, x, label=&#039;linear&#039;)  \nax.plot(x, x**2, label=&#039;quadratic&#039;)  \nax.plot(x, x**3, label=&#039;cubic&#039;)  \nax.set_xlabel(&#039;x label&#039;) \nax.set_ylabel(&#039;y label&#039;) \nax.set_title(&quot;Simple Plot&quot;)  \nax.legend() \nplt.savefig(&quot;temp3.svg&quot;, format=&quot;svg&quot;)  ##\u4fdd\u5b58\u6587\u4ef6\u8981\u4f4d\u4e8eplt.show\u524d\u9762\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u95ee\u9898\uff01\uff01\uff01\nplt.show()<\/code><\/pre>\n<div align = \"center\">\n    <img src = \"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192003904.svg\" alt = temp3 >\n<\/div>\n<h3>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a<\/h3>\n<pre><code class=\"language-python\">x = np.linspace(0, 2, 100)\nplt.plot(x, x, label=&#039;linear&#039;) \nplt.plot(x, x**2, label=&#039;quadratic&#039;)  \nplt.plot(x, x**3, label=&#039;cubic&#039;)\nplt.xlabel(&#039;x label&#039;)\nplt.ylabel(&#039;y label&#039;)\nplt.title(&quot;Simple Plot&quot;)\nplt.legend()\nplt.show()<\/code><\/pre>\n<div align = \"center\">\n<img src = \"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192033908.svg\" alt = \"temp4\" >\n<\/div>\n<h2>\u901a\u7528\u7ed8\u56fe\u6a21\u677f<\/h2>\n<pre><code class=\"language-python\">plt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u4e2d\u6587\u6807\u7b7e\nplt.rcParams[&#039;axes.unicode_minus&#039;] = False  # \u7528\u6765\u6b63\u5e38\u663e\u793a\u8d1f\u53f7<\/code><\/pre>\n<h3>\u9762\u5411\u5bf9\u8c61\u7684\u7ed8\u56fe\u6a21\u677f<\/h3>\n<pre><code class=\"language-python\"># step1 \u51c6\u5907\u6570\u636e\nx = np.linspace(0, 2, 100)\ny = x**2\n# step2 \u8bbe\u7f6e\u7ed8\u56fe\u6837\u5f0f\uff0c\u8fd9\u4e00\u6a21\u5757\u7684\u6269\u5c55\u53c2\u8003\u7b2c\u4e94\u7ae0\u8fdb\u4e00\u6b65\u5b66\u4e60\uff0c\u8fd9\u4e00\u6b65\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u6837\u5f0f\u4e5f\u53ef\u4ee5\u5728\u7ed8\u5236\u56fe\u50cf\u662f\u8fdb\u884c\u8bbe\u7f6e\nmpl.rc(&#039;lines&#039;, linewidth=4, linestyle=&#039;-.&#039;)\n# step3 \u5b9a\u4e49\u5e03\u5c40\uff0c \u8fd9\u4e00\u6a21\u5757\u7684\u6269\u5c55\u53c2\u8003\u7b2c\u4e09\u7ae0\u8fdb\u4e00\u6b65\u5b66\u4e60\nfig, ax = plt.subplots()  \n# step4 \u7ed8\u5236\u56fe\u50cf\uff0c \u8fd9\u4e00\u6a21\u5757\u7684\u6269\u5c55\u53c2\u8003\u7b2c\u4e8c\u7ae0\u8fdb\u4e00\u6b65\u5b66\u4e60\nax.plot(x, y, label=&#039;linear&#039;)  \n# step5 \u6dfb\u52a0\u6807\u7b7e\uff0c\u6587\u5b57\u548c\u56fe\u4f8b\uff0c\u8fd9\u4e00\u6a21\u5757\u7684\u6269\u5c55\u53c2\u8003\u7b2c\u56db\u7ae0\u8fdb\u4e00\u6b65\u5b66\u4e60\nax.set_xlabel(&#039;x label&#039;) \nax.set_ylabel(&#039;y label&#039;) \nax.set_title(&quot;Simple Plot&quot;)  \nax.legend() ;<\/code><\/pre>\n<div align = \"center\">\n<img src = \"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192022581.svg\" alt = \"temp5\">\n<\/div>\n<h3>pyplot\u7684\u7ed8\u56fe\u6a21\u677f<\/h3>\n<pre><code class=\"language-python\"># step1 \u51c6\u5907\u6570\u636e\nx = np.linspace(0, 2, 100)\ny = x**2\n#x\uff0cy\u8f74\u6570\u636e\nplt.plot(x, y, label=&#039;linear&#039;) \nplt.xlabel(&#039;x label&#039;)\nplt.ylabel(&#039;y label&#039;)\nplt.title(&quot;Simple Plot&quot;)\nplt.legend()\n# plt.savefig(&quot;temp6.svg&quot;, format=&quot;svg&quot;)\nplt.show()\n#\u56fe\u548c\u4e0a\u9762\u4e00\u6478\u4e00\u6837<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Matplotlib\u521d\u76f8\u8bc6 \u57fa\u7840 \u5bfc\u5165\u76f8\u5173\u5e93\u51fd\u6570 import matplotlib.pyplot as pl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,32],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-python-learn"],"_links":{"self":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":1,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":229,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/229"}],"wp:attachment":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}