{"id":205458,"date":"2025-05-29T14:51:45","date_gmt":"2025-05-29T06:51:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205458\/"},"modified":"2025-05-29T14:51:45","modified_gmt":"2025-05-29T06:51:45","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-python-%e5%9b%be%e8%a1%a8%e4%b8%ad%e7%9a%84-x-%e8%bd%b4%e5%88%bb%e5%ba%a6%e8%ae%be%e7%bd%ae%e4%b8%ba%e6%97%a5%e6%9c%9f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205458\/","title":{"rendered":"\u5982\u4f55\u5c06 Python \u56fe\u8868\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 Python \u56fe\u8868\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5c06 Python \u56fe\u8868\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733141380674da384dead6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u56fe\u8868\u8bbe\u7f6e x \u8f74\u523b\u5ea6\u4e3a\u65e5\u671f<\/strong><\/p>\n<p><strong>\u63d0\u95ee\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 pandas \u548c matplotlib \u521b\u5efa\u7684\u56fe\u8868\u4e2d\uff0c\u5982\u4f55\u5c06 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff08\u800c\u975e\u9ed8\u8ba4\u7684\u6570\u5b57\uff09\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u8981\u5c06 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff0c\u9700\u8981\u5bf9\u6570\u636e\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b\uff1a<\/strong>\u4f7f\u7528 pd.to_datetime() \u51fd\u6570\u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<pre>df['date'] = pd.to_datetime(df['date'])<\/pre>\n<ol>\n<li><strong>\u5c06\u7d22\u5f15\u8bbe\u7f6e\u4e3a\u65e5\u671f\u5217\uff1a<\/strong>\u4f7f\u7528 set_index() \u51fd\u6570\u5c06\u65e5\u671f\u5217\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<\/li>\n<\/ol>\n<pre>df = df.set_index(df['date'])<\/pre>\n<ol>\n<li><strong>\u7ed8\u5236\u56fe\u8868\uff1a<\/strong>\u4f7f\u7528 plot() \u65b9\u6cd5\u7ed8\u5236\u56fe\u8868\uff0c\u5e76\u4f7f\u7528 x \u53c2\u6570\u6307\u5b9a x \u8f74\u4f7f\u7528\u7684\u5217\u3002<\/li>\n<\/ol>\n<pre>df.plot.area(x='date')<\/pre>\n<p><strong>\u4ee3\u7801\u66f4\u65b0\uff1a<\/strong><\/p>\n<pre>import pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport datetime\n\nfileName = 'test_01.csv'\ndf = pd.read_csv(fileName)\n\ndf['date'] = pd.to_datetime(df['date'])\ndf = df.set_index(df['date'])\n\ndf.plot.area()\nplt.legend(loc='upper left', bbox_to_anchor=(1, 1))  \nplt.savefig(fileName.replace('.csv','.png'), bbox_inches='tight')<\/pre>\n<p>\u6b64\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u5c06\u7ed8\u5236\u5177\u6709\u65e5\u671f\u523b\u5ea6\u683c\u5f0f\u7684 x \u8f74\u7684\u56fe\u8868\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5c06 Python \u56fe\u8868\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 Python \u56fe\u8868\u4e2d\u7684 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205458","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205458"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205458\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}