{"id":1149532,"date":"2025-01-13T16:50:40","date_gmt":"2025-01-13T08:50:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149532.html"},"modified":"2025-01-13T16:50:43","modified_gmt":"2025-01-13T08:50:43","slug":"python%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149532.html","title":{"rendered":"python\u753b\u56fe\u5982\u4f55\u4f7f\u7528\u65e5\u671f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180225\/293d4853-7e0b-49d4-94d7-077c4f5031da.webp\" alt=\"python\u753b\u56fe\u5982\u4f55\u4f7f\u7528\u65e5\u671f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528\u65e5\u671f\u8fdb\u884c\u753b\u56fe\u901a\u5e38\u6d89\u53ca\u5230\u6570\u636e\u7684\u5904\u7406\u548c\u53ef\u89c6\u5316\u5de5\u5177\u7684\u5e94\u7528\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>matplotlib<\/code>\u548c<code>pandas<\/code>\u3002<strong>\u4f7f\u7528\u65e5\u671f\u6570\u636e\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u5177\u4fe1\u606f\u6027\u548c\u76f4\u89c2\u6027<\/strong>\u3001<strong>\u901a\u8fc7\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u53ef\u4ee5\u89c2\u5bdf\u8d8b\u52bf\u548c\u5468\u671f\u6027\u53d8\u5316<\/strong>\u3001<strong>\u53ef\u4ee5\u66f4\u597d\u5730\u5206\u6790\u548c\u9884\u6d4b\u672a\u6765\u7684\u60c5\u51b5<\/strong>\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u548c<code>pandas<\/code>\u6765\u5904\u7406\u548c\u7ed8\u5236\u5305\u542b\u65e5\u671f\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528matplotlib\u7ed8\u5236\u65e5\u671f\u56fe\u8868<\/h3>\n<\/p>\n<p><p><code>matplotlib<\/code>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u529f\u80fd\u6765\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u5305\u542b\u65e5\u671f\u6570\u636e\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u51c6\u5907\u597d\u5305\u542b\u65e5\u671f\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.dates as mdates<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>dates = pd.date_range(&#39;2023-01-01&#39;, periods=100)<\/p>\n<p>data = pd.Series(range(100), index=dates)<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;Value&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u7ed8\u56fe\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6e\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(df.index, df[&#39;Value&#39;])<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u671f\u683c\u5f0f<\/strong><\/h2>\n<p>plt.gca().xaxis.set_major_formatter(mdates.DateFormatter(&#39;%Y-%m-%d&#39;))<\/p>\n<p>plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=10))<\/p>\n<h2><strong>\u81ea\u52a8\u65cb\u8f6c\u65e5\u671f\u6807\u7b7e<\/strong><\/h2>\n<p>plt.gcf().autofmt_xdate()<\/p>\n<p>plt.title(&#39;Example Plot with Dates&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Value&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5904\u7406\u548c\u7ed8\u5236\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u7ed3\u5408<code>matplotlib<\/code>\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ed8\u5236\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u4f1a\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\u6570\u636e\u5e76\u89e3\u6790\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u89e3\u6790\u65e5\u671f<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;, parse_dates=[&#39;Date&#39;], index_col=&#39;Date&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5904\u7406\u6570\u636e\uff0c\u4f8b\u5982\u586b\u5145\u7f3a\u5931\u503c\u3001\u8ba1\u7b97\u79fb\u52a8\u5e73\u5747\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u586b\u5145\u7f3a\u5931\u503c<\/p>\n<p>df.fillna(method=&#39;ffill&#39;, inplace=True)<\/p>\n<h2><strong>\u8ba1\u7b977\u5929\u79fb\u52a8\u5e73\u5747<\/strong><\/h2>\n<p>df[&#39;7-day MA&#39;] = df[&#39;Value&#39;].rolling(window=7).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u56fe\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u7684\u5185\u7f6e\u7ed8\u56fe\u529f\u80fd\u6216\u7ed3\u5408<code>matplotlib<\/code>\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(10, 6))<\/p>\n<p>df[&#39;Value&#39;].plot(label=&#39;D<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ly Value&#39;)<\/p>\n<p>df[&#39;7-day MA&#39;].plot(label=&#39;7-day Moving Average&#39;)<\/p>\n<p>plt.title(&#39;Time Series Plot&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Value&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e0e\u9884\u6d4b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\uff0c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5206\u6790\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h4>1. \u8d8b\u52bf\u4e0e\u5b63\u8282\u6027\u5206\u89e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>statsmodels<\/code>\u5e93\u8fdb\u884c\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u5206\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from statsmodels.tsa.seasonal import seasonal_decompose<\/p>\n<h2><strong>\u8fdb\u884c\u5b63\u8282\u6027\u5206\u89e3<\/strong><\/h2>\n<p>result = seasonal_decompose(df[&#39;Value&#39;], model=&#39;additive&#39;, period=30)<\/p>\n<p>result.plot()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9884\u6d4b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528ARIMA\u6a21\u578b\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from statsmodels.tsa.arima.model import ARIMA<\/p>\n<h2><strong>\u62df\u5408ARIMA\u6a21\u578b<\/strong><\/h2>\n<p>model = ARIMA(df[&#39;Value&#39;], order=(5, 1, 0))<\/p>\n<p>fit = model.fit()<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>forecast = fit.forecast(steps=30)<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(df.index, df[&#39;Value&#39;], label=&#39;Observed&#39;)<\/p>\n<p>plt.plot(forecast.index, forecast, label=&#39;Forecast&#39;)<\/p>\n<p>plt.title(&#39;Time Series Forecast&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Value&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528\u65e5\u671f\u8fdb\u884c\u753b\u56fe\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u548c\u91cd\u8981\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u4f7f\u7528<code>matplotlib<\/code>\u548c<code>pandas<\/code>\u7b49\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u53ef\u89c6\u5316\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<strong>\u4f7f\u7528\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u9884\u6d4b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\u548c\u89c4\u5f8b<\/strong>\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u597d\u7684\u51b3\u7b56\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u5185\u5bb9\u80fd\u4e3a\u4f60\u5728\u5904\u7406\u548c\u7ed8\u5236\u5305\u542b\u65e5\u671f\u7684\u56fe\u8868\u65f6\u63d0\u4f9b\u6709\u7528\u7684\u53c2\u8003\u548c\u6307\u5bfc\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u65e5\u671f\u6570\u636e\u4ee5\u4fbf\u4e8e\u7ed8\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u65e5\u671f\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u65b9\u4fbf\u5730\u89e3\u6790\u548c\u7ba1\u7406\u65e5\u671f\u3002\u901a\u8fc7<code>pd.to_datetime()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002\u8fd9\u6837\uff0c\u7ed8\u56fe\u65f6\u5c31\u80fd\u66f4\u51c6\u786e\u5730\u8868\u793a\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u786e\u4fdd\u5728\u7ed8\u56fe\u524d\u5c06\u6570\u636e\u7684\u7d22\u5f15\u8bbe\u7f6e\u4e3a\u65e5\u671f\uff0c\u4ee5\u4fbf\u5229\u7528\u65f6\u95f4\u5e8f\u5217\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528Matplotlib\u7ed8\u5236\u65e5\u671f\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u65e5\u671f\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>matplotlib.dates<\/code>\u6a21\u5757\u6765\u683c\u5f0f\u5316\u65e5\u671f\u3002\u53ef\u4ee5\u901a\u8fc7<code>mdates.DateFormatter<\/code>\u6765\u5b9a\u4e49\u65e5\u671f\u7684\u663e\u793a\u683c\u5f0f\u3002\u6b64\u5916\uff0c<code>mdates.AutoDateLocator<\/code>\u53ef\u4ee5\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u65e5\u671f\u95f4\u9694\uff0c\u4ee5\u4f18\u5316\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u56fe\u5f62\u65f6\u4f7f\u7528\u9002\u5f53\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u4ee5\u907f\u514d\u56fe\u5f62\u62e5\u6324\u6216\u4fe1\u606f\u4e22\u5931\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u56fe\u4e2d\u6dfb\u52a0\u65e5\u671f\u8303\u56f4\u7684\u6807\u6ce8\uff1f<\/strong><br \/>\u5728Python\u7684\u7ed8\u56fe\u4e2d\uff0c\u6dfb\u52a0\u65e5\u671f\u8303\u56f4\u7684\u6807\u6ce8\u53ef\u4ee5\u4f7f\u7528<code>ax.axvspan()<\/code>\u51fd\u6570\u3002\u8fd9\u5141\u8bb8\u7528\u6237\u5728\u7279\u5b9a\u7684\u65e5\u671f\u8303\u56f4\u5185\u586b\u5145\u989c\u8272\uff0c\u4ee5\u7a81\u51fa\u663e\u793a\u67d0\u4e00\u65f6\u95f4\u6bb5\u7684\u4e8b\u4ef6\u6216\u6570\u636e\u53d8\u5316\u3002\u901a\u8fc7\u8bbe\u7f6e\u900f\u660e\u5ea6\u548c\u989c\u8272\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u6807\u6ce8\u66f4\u5177\u89c6\u89c9\u6548\u679c\uff0c\u540c\u65f6\u4e0d\u5f71\u54cd\u5176\u4ed6\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u65e5\u671f\u8fdb\u884c\u753b\u56fe\u901a\u5e38\u6d89\u53ca\u5230\u6570\u636e\u7684\u5904\u7406\u548c\u53ef\u89c6\u5316\u5de5\u5177\u7684\u5e94\u7528\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecmatplotlib\u548cpa [&hellip;]","protected":false},"author":3,"featured_media":1149540,"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\/1149532"}],"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=1149532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149532\/revisions"}],"predecessor-version":[{"id":1149543,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149532\/revisions\/1149543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149540"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}