{"id":71189,"date":"2024-02-02T10:48:36","date_gmt":"2024-02-02T02:48:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=71189"},"modified":"2024-02-02T10:48:37","modified_gmt":"2024-02-02T02:48:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e4%bd%bf%e7%94%a8matplotlib%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%8f%af%e8%a7%86%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/71189.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316"},"content":{"rendered":"<p>\u6458\u8981\uff1a\u5728Python\u4e2d\u5e94\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a<strong>1\u3001\u5b89\u88c5Matplotlib\u5e93\uff1b2\u3001\u5bfc\u5165\u76f8\u5173\u5e93\u4e0e\u6570\u636e\uff1b3\u3001\u521b\u5efa\u56fe\u8868\u5bf9\u8c61\uff1b4\u3001\u56fe\u5f62\u7ed8\u5236\uff1b5\u3001\u7f8e\u5316\u56fe\u8868\uff1b6\u3001\u663e\u793a\u4e0e\u4fdd\u5b58\u7ed3\u679c\u3002<\/strong>\u4e3a\u4e86\u6df1\u5165\u7406\u89e3\uff0c\u672c\u6587\u5c06\u91cd\u70b9\u8be6\u8ff0<strong>\u56fe\u5f62\u7ed8\u5236<\/strong>\u7684\u6280\u5de7\u4e0e\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662f\u53ef\u89c6\u5316\u4e2d\u6700\u4e3a\u6838\u5fc3\u7684\u73af\u8282\u3002<\/p>\n<h2><strong>\u4e00\u3001PYTHON\u53caMATPLOTLIB\u6982\u8ff0<\/strong><\/h2>\n<p>Python\u4f5c\u4e3a\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u6570\u636e\u5206\u6790\u4e0e\u53ef\u89c6\u5316\u9886\u57df\u5177\u6709\u91cd\u8981\u5730\u4f4d\u3002Matplotlib\u5e93\u5219\u662f\u5229\u7528Python\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u652f\u6301\u4f17\u591a\u56fe\u8868\u7c7b\u578b\u7684\u7ed8\u5236\u3002<\/p>\n<p>\u6570\u636e\u53ef\u89c6\u5316\u4e0d\u4ec5\u5bf9\u6570\u636e\u8fdb\u884c\u7f8e\u5316\uff0c\u66f4\u53ef\u4ee5\u63ed\u793a\u6570\u636e\u4e4b\u95f4\u7684\u5173\u7cfb\u548c\u7ed3\u6784\uff0c\u5229\u4e8e\u4eba\u4eec\u7406\u89e3\u548c\u6d1e\u5bdf\u3002\u56e0\u6b64\uff0c\u638c\u63e1Matplotlib\u7684\u4f7f\u7528\uff0c\u5bf9\u4efb\u4f55\u6570\u636e\u5206\u6790\u5e08\u800c\u8a00\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h2><strong>\u4e8c\u3001\u5b89\u88c5MATPLOTLIB<\/strong><\/h2>\n<p>\u4f7f\u7528Matplotlib\u524d\uff0c\u9700\u786e\u4fddPython\u73af\u5883\u5df2\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u4f7f\u7528pip\u5b89\u88c5\u5668\u6765\u5b89\u88c5Matplotlib\u5e93\u3002\u6267\u884c\u547d\u4ee4`pip install matplotlib`\u5373\u53ef\u5b8c\u6210\u5b89\u88c5\u8fc7\u7a0b\u3002<\/p>\n<h2><strong>\u4e09\u3001\u5e93\u7684\u5bfc\u5165\u4e0e\u6570\u636e\u7684\u51c6\u5907<\/strong><\/h2>\n<p>\u7ed8\u56fe\u524d\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\uff0c\u901a\u5e38\u8fd8\u4f1a\u5bfc\u5165\u6570\u636e\u5904\u7406\u5e93\u5982NumPy\u6216Pandas\u4ee5\u65b9\u4fbf\u6570\u636e\u64cd\u4f5c\u3002\u5bfc\u5165matplotlib.pyplot\u6a21\u5757\uff0c\u5e76\u7ea6\u5b9a\u4fd7\u6210\u5730\u5c06\u5176\u91cd\u547d\u540d\u4e3aplt\u3002\u83b7\u53d6\u6570\u636e\u662f\u7ed8\u56fe\u7684\u57fa\u7840\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u4ece\u591a\u79cd\u6765\u6e90\u8bfb\u53d6\u6570\u636e\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3001SQL\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<h2><strong>\u56db\u3001\u521b\u5efa\u56fe\u8868\u5bf9\u8c61<\/strong><\/h2>\n<p>\u521b\u5efa\u56fe\u8868\u5bf9\u8c61\uff0c\u7b80\u8a00\u4e4b\u5c31\u662f\u5efa\u7acb\u7ed8\u56fe\u7684\u57fa\u7840\u201c\u753b\u5e03\u201d\uff0c\u5728Matplotlib\u4e2d\u79f0\u4f5cfigure\u3002\u8c03\u7528`plt.figure()`\u5373\u53ef\u521b\u5efa\u65b0\u7684figure\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u53ef\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u8c03\u6574\u5176\u5927\u5c0f\u4e0e\u5206\u8fa8\u7387\u3002<\/p>\n<h2><strong>\u4e94\u3001\u56fe\u5f62\u7ed8\u5236<\/strong><\/h2>\n<p>\u7ed8\u56fe\u662f\u6570\u636e\u53ef\u89c6\u5316\u7684\u6838\u5fc3\u73af\u8282\uff0cMatplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u51fd\u6570\u3002<strong>\u5e38\u89c1\u7684\u7ed8\u56fe\u51fd\u6570\u5305\u62ec`plt.plot()`\u7528\u4e8e\u7ed8\u5236\u7ebf\u5f62\u56fe\uff0c`plt.bar()`\u7528\u4e8e\u67f1\u72b6\u56fe\uff0c`plt.scatter()`\u7528\u4e8e\u6563\u70b9\u56fe\u7b49\u3002<\/strong>\u7528\u6237\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u6027\u548c\u9700\u8981\u8868\u8fbe\u7684\u4fe1\u606f\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u56fe\u5f62\u8fdb\u884c\u7ed8\u5236\u3002<\/p>\n<p>\u9488\u5bf9\u56fe\u5f62\u7ed8\u5236\uff0c\u91cd\u70b9\u4ecb\u7ecd\u7ebf\u5f62\u56fe\u7684\u7ed8\u5236\u65b9\u6cd5\u3002\u7ebf\u5f62\u56fe\u5e38\u7528\u4e8e\u5c55\u73b0\u6570\u636e\u968f\u65f6\u95f4\u53d8\u5316\u7684\u8d8b\u52bf\u3002\u901a\u8fc7`plt.plot(x, y)`\u51fd\u6570\u53ef\u4ee5\u5b8c\u6210\u7ebf\u5f62\u56fe\u7684\u7ed8\u5236\uff0c\u5176\u4e2dx\u4e3a\u6a2a\u5750\u6807\u7684\u6570\u636e\u70b9\uff0cy\u4e3a\u7eb5\u5750\u6807\u7684\u6570\u636e\u70b9\u3002<\/p>\n<h2><strong>\u516d\u3001\u56fe\u8868\u7f8e\u5316<\/strong><\/h2>\n<p>\u9664\u4e86\u7b80\u5355\u7684\u56fe\u5f62\u7ed8\u5236\u5916\uff0cMatplotlib\u8fd8\u63d0\u4f9b\u591a\u79cd\u81ea\u5b9a\u4e49\u6837\u5f0f\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u53ef\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u3001\u7ebf\u578b\u3001\u6807\u8bb0\u3001\u56fe\u4f8b\u3001\u5750\u6807\u8f74\u3001\u6807\u9898\u7b49\u5bf9\u56fe\u5f62\u8fdb\u884c\u7f8e\u5316\u3002\u8fd9\u4e9b\u7ec6\u8282\u7684\u8c03\u6574\u80fd\u5927\u5927\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\u3002<\/p>\n<h2><strong>\u4e03\u3001\u7ed3\u679c\u7684\u5448\u73b0\u4e0e\u4fdd\u5b58<\/strong><\/h2>\n<p>\u5b8c\u6210\u7ed8\u5236\u540e\uff0c\u901a\u8fc7`plt.show()`\u53ef\u4ee5\u663e\u793a\u51fa\u56fe\u5f62\u3002\u82e5\u8981\u4fdd\u5b58\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528`plt.savefig()`\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002\u5728\u4fdd\u5b58\u65f6\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u5982PNG\u3001PDF\u3001SVG\u7b49\u3002<\/p>\n<p> <strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><strong>\u5982\u4f55\u4f7f\u7528Matplotlib\u5728Python\u4e2d\u521b\u5efa\u4e00\u5e45\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1f<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684plot\u51fd\u6570\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528plot\u51fd\u6570\u4f20\u5165\u6570\u636e\u70b9\u7684x\u548cy\u5750\u6807\u3002\u6700\u540e\uff0c\u4f7f\u7528show\u51fd\u6570\u663e\u793a\u56fe\u5f62\u5373\u53ef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Matplotlib\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u6807\u7b7e\u548c\u6807\u9898\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff1f<\/strong><\/p>\n<p>\u8981\u5728Matplotlib\u4e2d\u521b\u5efa\u5e26\u6709\u6807\u7b7e\u548c\u6807\u9898\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528xlabel\u548cylabel\u51fd\u6570\u6307\u5b9ax\u8f74\u548cy\u8f74\u7684\u6807\u7b7e\uff0c\u4f7f\u7528title\u51fd\u6570\u8bbe\u7f6e\u56fe\u8868\u7684\u6807\u9898\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Matplotlib\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u56fe\u7684\u590d\u6742\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff1f<\/strong><\/p>\n<p>\u8981\u5728Matplotlib\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u56fe\u7684\u590d\u6742\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528subplot\u51fd\u6570\u5c06\u6574\u4e2a\u56fe\u8868\u5206\u5272\u6210\u4e00\u4e2a\u4e2a\u5c0f\u7684\u5b50\u56fe\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u5b50\u56fe\u7684\u884c\u6570\u548c\u5217\u6570\uff0c\u7136\u540e\u4f7f\u7528\u5404\u79cd\u7ed8\u56fe\u51fd\u6570\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u60a8\u60f3\u8981\u5c55\u793a\u7684\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e00\u4e2a\u56fe\u8868\u4e2d\u5c55\u793a\u591a\u4e2a\u76f8\u5173\u7684\u6570\u636e\uff0c\u4f7f\u5f97\u6bd4\u8f83\u548c\u5206\u6790\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6458\u8981\uff1a\u5728Python\u4e2d\u5e94\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a1\u3001\u5b89\u88c5Matplotlib\u5e93\uff1b [&hellip;]","protected":false},"author":1,"featured_media":71191,"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\/71189"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=71189"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/71189\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/71191"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=71189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=71189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=71189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}