{"id":1051041,"date":"2024-12-31T14:12:58","date_gmt":"2024-12-31T06:12:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051041.html"},"modified":"2024-12-31T14:13:00","modified_gmt":"2024-12-31T06:13:00","slug":"python%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e6%97%a5%e6%9c%9f%e7%94%bb%e6%8a%98%e7%ba%bf%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051041.html","title":{"rendered":"Python\u5982\u4f55\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/79f733be-2e93-42a0-a68b-e148955d8a67.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe\" \/><\/p>\n<p><p> Python\u5982\u4f55\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001Matplotlib\u8fdb\u884c\u56fe\u5f62\u7ed8\u5236\u3001Seaborn\u8fdb\u884c\u7f8e\u5316\u3001Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u548cMatplotlib\u662f\u6700\u57fa\u7840\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Pandas\u548cMatplotlib\u753b\u6298\u7ebf\u56fe<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u548cMatplotlib\u662f\u6700\u57fa\u7840\u7684\u7ed8\u5236\u6298\u7ebf\u56fe\u7684\u65b9\u6cd5\u3002Pandas\u7528\u4e8e\u6570\u636e\u5904\u7406\uff0c\u800cMatplotlib\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u6570\u503c\u7684CSV\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u6765\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u786e\u4fdd\u65e5\u671f\u5217\u4e3adatetime\u7c7b\u578b<\/strong><\/h2>\n<p>data[&#39;date&#39;] = pd.to_datetime(data[&#39;date&#39;])<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u671f\u5217\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>data.set_index(&#39;date&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e00\u6b65\u9aa4\u786e\u4fdd\u65e5\u671f\u5217\u88ab\u6b63\u786e\u89e3\u6790\u4e3a\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u6570\u636e\u6846\u7684\u7d22\u5f15\uff0c\u4ee5\u4fbf\u6211\u4eec\u7a0d\u540e\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7ed8\u5236\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.plot(data.index, data[&#39;value&#39;], marker=&#39;o&#39;, linestyle=&#39;-&#39;)<\/p>\n<p>plt.title(&#39;Line Chart by Date&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Value&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.plot<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u6298\u7ebf\u56fe\u3002<code>data.index<\/code>\u8868\u793a\u65e5\u671f\uff0c<code>data[&#39;value&#39;]<\/code>\u8868\u793a\u6570\u503c\u3002<code>marker=&#39;o&#39;<\/code>\u7528\u4e8e\u5728\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0a\u7ed8\u5236\u4e00\u4e2a\u5706\u5708\uff0c<code>linestyle=&#39;-&#39;<\/code>\u7528\u4e8e\u7ed8\u5236\u5b9e\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u7f8e\u5316<\/p>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u66f4\u7b80\u5355\u7684API\u3002<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5904\u7406\u90e8\u5206\u4e0e\u4f7f\u7528Pandas\u548cMatplotlib\u7684\u65b9\u6cd5\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Seaborn\u7ed8\u5236\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>sns.lineplot(x=data.index, y=data[&#39;value&#39;])<\/p>\n<p>plt.title(&#39;Line Chart by Date&#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><p>Seaborn\u7684<code>lineplot<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u5904\u7406\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u4e3a\u56fe\u8868\u6dfb\u52a0\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a<\/p>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5904\u7406\u90e8\u5206\u4ecd\u7136\u4e0e\u524d\u9762\u7684\u65b9\u6cd5\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Bokeh\u7ed8\u5236\u4ea4\u4e92\u5f0f\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<p>output_notebook()<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>p = figure(x_axis_type=&#39;datetime&#39;, title=&#39;Line Chart by Date&#39;, plot_height=400, plot_width=800)<\/p>\n<p>p.line(data.index, data[&#39;value&#39;], legend_label=&#39;Value&#39;, line_width=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Bokeh\u7684<code>figure<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\uff0c<code>x_axis_type=&#39;datetime&#39;<\/code>\u7528\u4e8e\u8bbe\u7f6ex\u8f74\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<code>p.line<\/code>\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528Pandas\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5904\u7406\uff0c\u518d\u4f7f\u7528Seaborn\u8fdb\u884c\u7f8e\u5316\uff0c\u6700\u540e\u4f7f\u7528Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>1. \u7efc\u5408\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5148\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u786e\u4fdd\u65e5\u671f\u5217\u4e3adatetime\u7c7b\u578b<\/strong><\/h2>\n<p>data[&#39;date&#39;] = pd.to_datetime(data[&#39;date&#39;])<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u671f\u5217\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>data.set_index(&#39;date&#39;, inplace=True)<\/p>\n<h2><strong>\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406<\/strong><\/h2>\n<p>data.fillna(method=&#39;ffill&#39;, inplace=True)<\/p>\n<p>data[&#39;value&#39;] = data[&#39;value&#39;].rolling(window=7).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7efc\u5408\u6570\u636e\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Seaborn\u8fdb\u884c\u7f8e\u5316\uff0c\u518d\u4f7f\u7528Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<p>output_notebook()<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u9759\u6001\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>sns.lineplot(x=data.index, y=data[&#39;value&#39;])<\/p>\n<p>plt.title(&#39;Line Chart by Date&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Value&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4f7f\u7528Bokeh\u7ed8\u5236\u4ea4\u4e92\u5f0f\u56fe<\/strong><\/h2>\n<p>p = figure(x_axis_type=&#39;datetime&#39;, title=&#39;Line Chart by Date&#39;, plot_height=400, plot_width=800)<\/p>\n<p>p.line(data.index, data[&#39;value&#39;], legend_label=&#39;Value&#39;, line_width=2)<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7ed3\u5408\u4e86Pandas\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3001Seaborn\u7684\u7f8e\u5316\u529f\u80fd\u548cBokeh\u7684\u4ea4\u4e92\u5f0f\u5c55\u793a\u80fd\u529b\uff0c\u4f7f\u5f97\u6570\u636e\u53ef\u89c6\u5316\u66f4\u52a0\u4e30\u5bcc\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528Pandas\u548cMatplotlib\u7684\u57fa\u7840\u65b9\u6cd5\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u7f8e\u5316\u7684\u65b9\u6cd5\u3001\u4f7f\u7528Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u5c55\u793a\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u7efc\u5408\u5b9e\u8df5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u80fd\u591f\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u6280\u672f\uff0c\u521b\u5efa\u51fa\u4e13\u4e1a\u3001\u4e30\u5bcc\u4e14\u7f8e\u89c2\u7684\u6298\u7ebf\u56fe\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\u683c\u5f0f\u4ee5\u4fbf\u7ed8\u5236\u6298\u7ebf\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u65e5\u671f\u683c\u5f0f\u901a\u5e38\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c06\u65e5\u671f\u6570\u636e\u8f6c\u6362\u4e3a<code>datetime<\/code>\u683c\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>pd.to_datetime()<\/code>\u51fd\u6570\u8f7b\u677e\u5b9e\u73b0\u3002\u786e\u4fdd\u5c06\u65e5\u671f\u5217\u8bbe\u7f6e\u4e3a\u7d22\u5f15\uff0c\u8fd9\u6837\u5728\u7ed8\u5236\u6298\u7ebf\u56fe\u65f6\uff0cX\u8f74\u5c06\u81ea\u52a8\u8bc6\u522b\u4e3a\u65e5\u671f\u3002<\/p>\n<p><strong>\u7ed8\u5236\u6298\u7ebf\u56fe\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u7ed8\u56fe\u5e93\u53ef\u4ee5\u521b\u5efa\u6298\u7ebf\u56fe\uff0c\u5305\u62ec<code>Matplotlib<\/code>\u3001<code>Seaborn<\/code>\u548c<code>Plotly<\/code>\u3002<code>Matplotlib<\/code>\u662f\u6700\u57fa\u7840\u7684\u9009\u62e9\uff0c\u9002\u5408\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1b<code>Seaborn<\/code>\u5728\u89c6\u89c9\u6548\u679c\u4e0a\u66f4\u4e3a\u5438\u5f15\uff0c\u9002\u5408\u5c55\u793a\u6570\u636e\u5173\u7cfb\uff1b\u800c<code>Plotly<\/code>\u5219\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u9002\u5408\u9700\u8981\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u573a\u666f\u3002\u9009\u62e9\u9002\u5408\u60a8\u9700\u6c42\u7684\u5e93\u53ef\u4ee5\u63d0\u5347\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7f3a\u5931\u7684\u65e5\u671f\u6570\u636e\u5728\u7ed8\u5236\u6298\u7ebf\u56fe\u65f6\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u6298\u7ebf\u56fe\u4e4b\u524d\uff0c\u5904\u7406\u7f3a\u5931\u7684\u65e5\u671f\u6570\u636e\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u4e2d\u7684<code>resample()<\/code>\u65b9\u6cd5\u5bf9\u65e5\u671f\u8fdb\u884c\u91cd\u65b0\u91c7\u6837\uff0c\u4ee5\u586b\u8865\u7f3a\u5931\u65e5\u671f\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u7ebf\u6027\u63d2\u503c\u6216\u4f7f\u7528\u524d\u5411\u586b\u5145\uff08<code>ffill<\/code>\uff09\u6765\u586b\u8865\u6570\u636e\u7f3a\u5931\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6298\u7ebf\u56fe\u7684\u8fde\u7eed\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe \u4f7f\u7528Python\u6839\u636e\u65e5\u671f\u753b\u6298\u7ebf\u56fe\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001Ma [&hellip;]","protected":false},"author":3,"featured_media":1051045,"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\/1051041"}],"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=1051041"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051041\/revisions"}],"predecessor-version":[{"id":1051048,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051041\/revisions\/1051048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051045"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}