{"id":179102,"date":"2024-05-08T20:05:58","date_gmt":"2024-05-08T12:05:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179102.html"},"modified":"2024-05-08T20:06:08","modified_gmt":"2024-05-08T12:06:08","slug":"python-matplotlib%e6%97%a5%e6%9c%9f%e9%a2%a0%e5%80%92%ef%bc%8c%e6%98%af%e4%bb%80%e4%b9%88%e5%8e%9f%e5%9b%a0%e9%80%a0%e6%88%90%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179102.html","title":{"rendered":"python matplotlib\u65e5\u671f\u98a0\u5012\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u9020\u6210\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063814\/2fee24c3-e72d-48ac-ae72-dc32d1ba7774.webp\" alt=\"python matplotlib\u65e5\u671f\u98a0\u5012\uff0c\u662f\u4ec0\u4e48\u539f\u56e0\u9020\u6210\u7684\" \/><\/p>\n<p><p><strong>Python Matplotlib\u65e5\u671f\u98a0\u5012<\/strong>\u901a\u5e38\u662f\u56e0\u4e3a\u6570\u636e\u8f93\u5165\u9519\u8bef\u3001\u65e5\u671f\u683c\u5f0f\u4e0d\u5339\u914d\u3001\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u6392\u5e8f\u51fd\u6570\u6216\u8bef\u7528\u6570\u636e\u5904\u7406\u65b9\u6cd5\u3002\u5176\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u5728\u5904\u7406\u65e5\u671f\u6570\u636e\u65f6\uff0c<strong>\u6ca1\u6709\u6309\u7167\u6b63\u786e\u7684\u65f6\u95f4\u987a\u5e8f\u5bf9\u65e5\u671f\u6570\u636e\u8fdb\u884c\u6392\u5e8f<\/strong>\uff0c\u5bfc\u81f4\u5728\u7ed8\u56fe\u65f6\u663e\u793a\u51fa\u98a0\u5012\u7684\u65e5\u671f\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff0c\u5982\u679c\u60a8\u662f\u5728\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u7136\u540e\u4f7f\u7528Matplotlib\u8fdb\u884c\u7ed8\u56fe\uff0c\u53ef\u80fd\u5728\u5c06\u65e5\u671f\u6570\u636e\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3apandas\u7684DateTime\u5bf9\u8c61\u4e4b\u540e\uff0c\u6ca1\u6709\u5bf9\u8f6c\u6362\u540e\u7684\u65e5\u671f\u8fdb\u884c\u9002\u5f53\u7684\u6392\u5e8f\u3002\u5728pandas\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528<code>sort_values()<\/code>\u65b9\u6cd5\u6765\u5bf9\u65e5\u671f\u8fdb\u884c\u6392\u5e8f\uff0c\u5982\u679c\u6f0f\u6389\u4e86\u8fd9\u4e2a\u6b65\u9aa4\uff0c\u6216\u8005\u9519\u8bef\u5730\u6309\u7167\u964d\u5e8f\u800c\u975e\u5347\u5e8f\u6392\u5217\uff0c\u90a3\u4e48\u5728\u7ed8\u5236\u56fe\u8868\u65f6\u4f1a\u51fa\u73b0\u65e5\u671f\u987a\u5e8f\u98a0\u5012\u7684\u73b0\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u7684\u6b63\u6587\u5c06\u8be6\u7ec6\u8ba8\u8bba\u53ef\u80fd\u5bfc\u81f4Matplotlib\u65e5\u671f\u98a0\u5012\u7684\u5404\u79cd\u539f\u56e0\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u6570\u636e\u8f93\u5165\u9519\u8bef<\/h3>\n<\/p>\n<p><p>Matplotlib\u4f5c\u4e3a\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u5176\u6570\u636e\u6765\u6e90\u4e8e\u7528\u6237\u8f93\u5165\uff0c\u5f53\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u672c\u8eab\u5c31\u662f\u98a0\u5012\u7684\uff0c\u81ea\u7136\u5728\u56fe\u8868\u4e2d\u4e5f\u4f1a\u8868\u73b0\u4e3a\u98a0\u5012\u3002\u68c0\u67e5\u539f\u59cb\u6570\u636e\u662f\u5426\u6309\u7167\u6b63\u786e\u7684\u65f6\u95f4\u987a\u5e8f\u6392\u5217\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u65e5\u671f\u683c\u5f0f\u4e0d\u5339\u914d<\/h3>\n<\/p>\n<p><p>\u65e5\u671f\u6570\u636e\u53ef\u80fd\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u5b58\u5728\uff0c\u5982&quot;YYYY\/MM\/DD&quot;\u3001&quot;MM\/DD\/YYYY&quot;\u7b49\u3002\u5f53Matplotlib\u63a5\u6536\u5230\u4e0d\u5339\u914d\u7684\u65e5\u671f\u683c\u5f0f\u65f6\uff0c\u53ef\u80fd\u4e0d\u4f1a\u6309\u7167\u671f\u671b\u7684\u65b9\u5f0f\u6e32\u67d3\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h4>1. \u786e\u8ba4\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u786e\u8ba4\u8f93\u5165Matplotlib\u7684\u65e5\u671f\u683c\u5f0f\u4e0e\u9884\u671f\u683c\u5f0f\u4e00\u81f4\u3002\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>datetime<\/code>\u6a21\u5757\u6765\u89e3\u6790\u548c\u91cd\u65b0\u683c\u5f0f\u5316\u65e5\u671f\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u786e\u7684\u65e5\u671f\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u5bf9\u65e5\u671f\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u5bf9\u5176\u8fdb\u884c\u6b63\u786e\u7684\u8f6c\u6362\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528pandas\u7684<code>to_datetime()<\/code>\u51fd\u6570\u786e\u4fdd\u6240\u6709\u65e5\u671f\u6570\u636e\u4ee5\u7edf\u4e00\u7684\u683c\u5f0f\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5373\u4f7f\u65e5\u671f\u683c\u5f0f\u6b63\u786e\uff0c\u5982\u679c\u5904\u7406\u6570\u636e\u65f6\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u6700\u7ec8\u7684\u56fe\u50cf\u663e\u793a\u51fa\u98a0\u5012\u7684\u65e5\u671f\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6b63\u786e\u7684\u6392\u5e8f\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pandas\u6216NumPy\u7b49\u5de5\u5177\u65f6\uff0c\u5e94\u4f7f\u7528\u6b63\u786e\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u5e76\u786e\u4fdd\u6309\u65f6\u95f4\u987a\u5e8f\uff08\u5347\u5e8f\uff09\u5bf9\u65e5\u671f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2. \u6821\u9a8c\u6392\u5e8f\u540e\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u7ed8\u56fe\u4e4b\u524d\uff0c\u91cd\u65b0\u68c0\u67e5\u6570\u636e\u662f\u5426\u6309\u671f\u671b\u7684\u987a\u5e8f\u6392\u5217\u597d\u3002\u53ef\u4ee5\u5728\u6392\u5e8f\u4e4b\u540e\u8f93\u51fa\u6570\u636e\u7684\u5934\u90e8\u548c\u5c3e\u90e8\u6765\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bef\u7528\u6570\u636e\u5904\u7406\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9884\u5904\u7406\u9636\u6bb5\uff0c\u4e00\u4e9b\u884c\u4e3a\u53ef\u80fd\u4f1a\u610f\u5916\u7834\u574f\u65e5\u671f\u7684\u987a\u5e8f\uff0c\u5982\u5408\u5e76\u6570\u636e\u96c6\u3001\u8fdb\u884c\u6570\u636e\u96c6\u7684\u5207\u7247\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6ce8\u610f\u6570\u636e\u5904\u7406\u7684\u987a\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728\u5408\u5e76\u3001\u5207\u7247\u6216\u662f\u4fee\u6539\u6570\u636e\u96c6\u7684\u8fc7\u7a0b\u4e2d\u8981\u5c0f\u5fc3\uff0c\u786e\u4fdd\u8fd9\u4e9b\u64cd\u4f5c\u4e0d\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u65f6\u95f4\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2. \u907f\u514d\u98a0\u5012\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u7ffb\u8f6c\u6216\u98a0\u5012\u64cd\u4f5c\u65f6\uff08\u6bd4\u5982\u4f7f\u7528<code>.iloc[::-1]<\/code>\uff09\uff0c\u8981\u975e\u5e38\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u662f\u5bfc\u81f4\u65e5\u671f\u98a0\u5012\u7684\u76f4\u63a5\u539f\u56e0\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u5904\u7406\u597d\u8fd9\u4e9b\u95ee\u9898\u4e4b\u540e\uff0c\u901a\u5e38\u53ef\u4ee5\u89e3\u51b3Matplotlib\u65e5\u671f\u98a0\u5012\u7684\u73b0\u8c61\u3002\u7136\u800c\uff0c\u5982\u679c\u60c5\u51b5\u6bd4\u4ee5\u4e0a\u63cf\u8ff0\u7684\u66f4\u52a0\u590d\u6742\uff0c\u53ef\u80fd\u9700\u8981\u6df1\u5165\u68c0\u67e5\u5177\u4f53\u7684\u6570\u636e\u5904\u7406\u6d41\u7a0b\u548c\u7ed8\u56fe\u4ee3\u7801\uff0c\u624d\u80fd\u627e\u5230\u95ee\u9898\u7684\u6839\u6e90\u5e76\u52a0\u4ee5\u89e3\u51b3\u3002\u5728\u6bcf\u4e00\u6b21\u7684\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\u73af\u8282\u4fdd\u6301\u8b66\u89c9\uff0c\u786e\u4fdd\u6240\u6709\u6b65\u9aa4\u90fd\u662f\u6b63\u786e\u65e0\u8bef\u7684\uff0c\u5bf9\u4e8e\u751f\u6210\u51c6\u786e\u7684\u6570\u636e\u56fe\u8868\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48\u5728Python\u7684Matplotlib\u4e2d\u65e5\u671f\u4f1a\u98a0\u5012\uff1f<\/strong><br \/>\n\u5728Matplotlib\u4e2d\uff0c\u65e5\u671f\u98a0\u5012\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\u9020\u6210\u7684\u3002<\/p>\n<p><strong>\u65e5\u671f\u98a0\u5012\u65f6\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\n\u5982\u679c\u60a8\u5728Matplotlib\u4e2d\u9047\u5230\u65e5\u671f\u98a0\u5012\u7684\u95ee\u9898\uff0c\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u51e0\u79cd\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u6570\u636e\u662f\u5426\u6309\u7167\u6b63\u786e\u7684\u987a\u5e8f\u6392\u5e8f<\/strong>\uff1a\u786e\u4fdd\u60a8\u7684\u65e5\u671f\u6570\u636e\u6309\u7167\u6b63\u786e\u7684\u987a\u5e8f\u6392\u5217\uff0c\u4ee5\u4fbf\u6b63\u786e\u7ed8\u5236\u56fe\u8868\u3002<\/li>\n<li><strong>\u67e5\u770b\u65e5\u671f\u683c\u5f0f\u662f\u5426\u6b63\u786e<\/strong>\uff1a\u68c0\u67e5\u65e5\u671f\u7684\u683c\u5f0f\u662f\u5426\u7b26\u5408Matplotlib\u6240\u9700\u7684\u683c\u5f0f\uff0c\u4f8b\u5982&quot;%Y-%m-%d&quot;\u3002<\/li>\n<li><strong>\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u65e5\u671f\u8f6c\u6362<\/strong>\uff1a\u5982\u679c\u60a8\u7684\u65e5\u671f\u6570\u636e\u662f\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684datetime\u6a21\u5757\u5c06\u5176\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u8fdb\u884c\u7ed8\u56fe\u3002<\/li>\n<li><strong>\u9006\u8f6cx\u8f74<\/strong>\uff1a\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ex\u8f74\u7684<code>plt.gca().invert_xaxis()<\/code>\u6765\u9006\u8f6c\u65e5\u671f\u7684\u663e\u793a\u987a\u5e8f\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u9884\u9632Matplotlib\u4e2d\u7684\u65e5\u671f\u98a0\u5012\u95ee\u9898\uff1f<\/strong><br \/>\n\u4e3a\u4e86\u907f\u514d\u65e5\u671f\u98a0\u5012\u95ee\u9898\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u9884\u9632\u63aa\u65bd\uff1a<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u6570\u636e\u6e90<\/strong>\uff1a\u5728\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u68c0\u67e5\u60a8\u7684\u65e5\u671f\u6570\u636e\u6e90\u662f\u5426\u5df2\u7ecf\u6309\u7167\u6b63\u786e\u7684\u987a\u5e8f\u6392\u5e8f\u3002<\/li>\n<li><strong>\u89c4\u8303\u65e5\u671f\u683c\u5f0f<\/strong>\uff1a\u786e\u4fdd\u60a8\u7684\u65e5\u671f\u6570\u636e\u91c7\u7528\u7edf\u4e00\u7684\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u683c\u5f0f\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/li>\n<li><strong>\u4f7f\u7528\u65e5\u671f\u5904\u7406\u51fd\u6570<\/strong>\uff1aMatplotlib\u63d0\u4f9b\u4e86\u8bb8\u591a\u65e5\u671f\u5904\u7406\u51fd\u6570\uff0c\u4f8b\u5982<code>date2num()<\/code>\u548c<code>num2date()<\/code>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5904\u7406\u65e5\u671f\u6570\u636e\u3002<\/li>\n<li><strong>\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u53ef\u4ee5\u8fdb\u884c\u5fc5\u8981\u7684\u6570\u636e\u9884\u5904\u7406\uff0c\u4f8b\u5982\u5bf9\u65e5\u671f\u8fdb\u884c\u6392\u5e8f\u548c\u683c\u5f0f\u8f6c\u6362\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u3002<\/li>\n<\/ol>\n<p>\u5e0c\u671b\u4ee5\u4e0a\u89e3\u7b54\u80fd\u591f\u5e2e\u52a9\u60a8\u89e3\u51b3Matplotlib\u4e2d\u65e5\u671f\u98a0\u5012\u7684\u95ee\u9898\u3002\u5982\u679c\u60a8\u8fd8\u6709\u5176\u4ed6\u7591\u95ee\uff0c\u8bf7\u968f\u65f6\u5411\u6211\u4eec\u63d0\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python Matplotlib\u65e5\u671f\u98a0\u5012\u901a\u5e38\u662f\u56e0\u4e3a\u6570\u636e\u8f93\u5165\u9519\u8bef\u3001\u65e5\u671f\u683c\u5f0f\u4e0d\u5339\u914d\u3001\u9519\u8bef\u5730\u4f7f\u7528\u4e86\u6392\u5e8f\u51fd\u6570\u6216\u8bef\u7528\u6570 [&hellip;]","protected":false},"author":3,"featured_media":179115,"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\/179102"}],"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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=179102"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179102\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179115"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}