{"id":204937,"date":"2025-05-29T11:59:40","date_gmt":"2025-05-29T03:59:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204937\/"},"modified":"2025-05-29T11:59:40","modified_gmt":"2025-05-29T03:59:40","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-python-%e5%9b%be%e5%83%8f%e7%bb%98%e5%88%b6%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%e6%a0%bc%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204937\/","title":{"rendered":"\u5982\u4f55\u5c06 Python \u56fe\u50cf\u7ed8\u5236\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 Python \u56fe\u50cf\u7ed8\u5236\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u5c06 Python \u56fe\u50cf\u7ed8\u5236\u4e2d\u7684 x \u8f74\u523b\u5ea6\u8bbe\u7f6e\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732019799673c8657392fa.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u56fe\u50cf\u7ed8\u5236\u4e2d x \u8f74\u7684\u81ea\u5b9a\u4e49\u8bbe\u5b9a<\/strong><\/p>\n<p>\u5728\u56fe\u50cf\u7ed8\u5236\u4e2d\uff0cx \u8f74\u7684\u523b\u5ea6\u901a\u5e38\u8868\u793a\u65f6\u95f4\u6216\u65e5\u671f\u7b49\u8fde\u7eed\u503c\u3002\u4e3a\u4e86\u5728\u56fe\u8868\u4e2d\u6e05\u6670\u5730\u5c55\u793a\u8fd9\u4e9b\u523b\u5ea6\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u81ea\u5b9a\u4e49\u8bbe\u5b9a\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u6570\u503c\u6570\u636e\u7684 csv \u6587\u4ef6\uff0c\u9700\u8981\u751f\u6210\u4e00\u4e2a\u56fe\u8868\uff0c\u5176\u4e2d x \u8f74\u8868\u793a\u65e5\u671f\u3002\u4f46\u662f\uff0c\u9ed8\u8ba4\u7684 x \u8f74\u523b\u5ea6\u4e3a\u8fde\u7eed\u7f16\u53f7\uff0c\u65e0\u6cd5\u76f4\u63a5\u663e\u793a\u5177\u4f53\u7684\u65e5\u671f\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u5c06 x \u8f74\u523b\u5ea6\u66f4\u6539\u4e3a\u65e5\u671f\uff0c\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u5c06\u65e5\u671f\u5217\u8bbe\u7f6e\u4e3a\u4e3b\u952e\uff1a<\/strong>\u4f7f\u7528 set_index() \u65b9\u6cd5\u5c06 date \u5217\u8bbe\u7f6e\u4e3a dataframe \u7684\u4e3b\u952e\u3002<\/li>\n<li><strong>\u4f7f\u7528 matplotlib.dates \u6a21\u5757\uff1a<\/strong>\u5bfc\u5165 matplotlib.dates \u6a21\u5757\uff0c\u5e76\u4f7f\u7528 dateformatter \u7c7b\u5c06 x \u8f74\u523b\u5ea6\u683c\u5f0f\u5316\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/li>\n<\/ol>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>import pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport matplotlib.dates as mdates\nimport datetime\n\nfileName = 'test_01.csv'\ndf = pd.read_csv(fileName)\n\n# \u8bbe\u7f6e\u65e5\u671f\u5217\u4e3a\u4e3b\u952e\ndf.set_index(df['date'], inplace=True)\n\n# \u4f7f\u7528 DateFormatter \u683c\u5f0f\u5316 x \u8f74\u523b\u5ea6\nax = df[['A','B','C']].plot.area()\nax.xaxis.set_major_formatter(mdates.DateFormatter('%Y\/%m\/%d'))\nax.xaxis.set_major_locator(mdates.DayLocator(interval=1))\nplt.legend(loc='upper left', bbox_to_anchor=(1, 1))  \nplt.savefig(fileName.replace('.csv','.png'), bbox_inches='tight')<\/pre>\n<p><strong>\u6548\u679c<\/strong><\/p>\n<p>\u4fee\u6539\u540e\uff0c\u56fe\u8868\u4e2d x \u8f74\u7684\u523b\u5ea6\u5c06\u663e\u793a\u4e3a\u5177\u4f53\u65e5\u671f\uff0c\u4f8b\u5982 &#8220;2020\/4\/1&#8243;\uff0c\u66f4\u4fbf\u4e8e\u7406\u89e3\u56fe\u50cf\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 Python \u56fe\u50cf\u7ed8\u5236\u4e2d&#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-204937","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204937","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=204937"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204937\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}