{"id":1072170,"date":"2025-01-08T11:13:49","date_gmt":"2025-01-08T03:13:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072170.html"},"modified":"2025-01-08T11:13:50","modified_gmt":"2025-01-08T03:13:50","slug":"python%e5%87%ba%e7%9a%84%e5%9b%be%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%ba%e6%95%b0%e6%8d%ae%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072170.html","title":{"rendered":"python\u51fa\u7684\u56fe\u5982\u4f55\u5bfc\u51fa\u6570\u636e\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102455\/c6c69e7f-5f1a-4b64-bcd3-ad3057bfd80e.webp\" alt=\"python\u51fa\u7684\u56fe\u5982\u4f55\u5bfc\u51fa\u6570\u636e\u529f\u80fd\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u7684\u56fe\u8868\u6570\u636e\u53ef\u4ee5\u5bfc\u51fa\u6210\u591a\u79cd\u683c\u5f0f\u3001\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u683c\u5f0f\u3002\u4e3a\u4e86\u8be6\u7ec6\u5bfc\u51fa\u6570\u636e\uff0c\u4e0b\u9762\u6211\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u51e0\u4e2a\u5e38\u89c1\u7684Python\u5e93\u6765\u5bfc\u51fa\u56fe\u8868\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5bfc\u51faPython\u751f\u6210\u7684\u56fe\u8868\u6570\u636e\uff0c\u901a\u5e38\u4f7f\u7528\u7684\u5e93\u5305\u62ecMatplotlib\u3001Pandas\u3001Numpy\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u5e93\u6765\u521b\u5efa\u56fe\u8868\uff0c\u5e76\u5c06\u6570\u636e\u5bfc\u51fa\u6210\u4e0d\u540c\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Matplotlib<\/strong>\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684Python\u7ed8\u56fe\u5e93\uff0c\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u5c3d\u7ba1Matplotlib\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u5176\u4ed6\u5e93\u6765\u5bfc\u51fa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u51fa\u6570\u636e\u4e3aCSV\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u5e76\u7ed8\u5236\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>plt.plot(df[&#39;x&#39;], df[&#39;y&#39;])<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)<\/p>\n<p>plt.title(&#39;Sample Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_csv(&#39;plot_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Pandas<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u901a\u8fc7Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u51fa\u56fe\u8868\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u51fa\u6570\u636e\u4e3aExcel\u683c\u5f0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;plot_data.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u5904\u7406\u6570\u636e\u5e76\u5bfc\u51fa<\/h3>\n<\/p>\n<p><p><strong>Numpy<\/strong>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Numpy\u5904\u7406\u6570\u636e\u5e76\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u51fa\u6570\u636e\u4e3aJSON\u683c\u5f0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import json<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: np.array([1, 2, 3, 4, 5]),<\/p>\n<p>    &#39;y&#39;: np.array([2, 3, 5, 7, 11])<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>data_dict = {key: list(value) for key, value in data.items()}<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;plot_data.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(data_dict, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Seaborn\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Seaborn<\/strong>\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\u3002Seaborn\u901a\u5e38\u4e0ePandas\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efaSeaborn\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(data=df, x=&#39;x&#39;, y=&#39;y&#39;)<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)<\/p>\n<p>plt.title(&#39;Sample Seaborn Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u5bfc\u51fa\u6570\u636e\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;seaborn_plot_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Plotly\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Plotly<\/strong>\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u56fe\u8868\u3002\u901a\u8fc7Plotly\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe\u8868\u6570\u636e\u5bfc\u51fa\u4e3a\u4e0d\u540c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efaPlotly\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.line(df, x=&#39;x&#39;, y=&#39;y&#39;, title=&#39;Sample Plotly Plot&#39;)<\/p>\n<p>fig.show()<\/p>\n<h2><strong>\u5bfc\u51fa\u6570\u636e\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;plotly_plot_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Alt<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Altair<\/strong>\u662f\u4e00\u4e2a\u58f0\u660e\u5f0f\u7edf\u8ba1\u53ef\u89c6\u5316\u5e93\uff0c\u5b83\u57fa\u4e8eVega\u548cVega-Lite\u6784\u5efa\u3002Altair\u4f7f\u5f97\u521b\u5efa\u590d\u6742\u7684\u56fe\u8868\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import altair as alt<\/p>\n<p>import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efaAltair\u56fe\u8868<\/strong><\/h2>\n<p>chart = alt.Chart(df).mark_line().encode(<\/p>\n<p>    x=&#39;x&#39;,<\/p>\n<p>    y=&#39;y&#39;<\/p>\n<p>).properties(<\/p>\n<p>    title=&#39;Sample Altair Plot&#39;<\/p>\n<p>)<\/p>\n<p>chart.show()<\/p>\n<h2><strong>\u5bfc\u51fa\u6570\u636e\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;altair_plot_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Bokeh\u5bfc\u51fa\u56fe\u8868\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>Bokeh<\/strong>\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002Bokeh\u4e0ePandas\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bfc\u51fa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show, output_file<\/p>\n<p>import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efaBokeh\u56fe\u8868<\/strong><\/h2>\n<p>p = figure(title=&#39;Sample Bokeh Plot&#39;, x_axis_label=&#39;X Axis&#39;, y_axis_label=&#39;Y Axis&#39;)<\/p>\n<p>p.line(df[&#39;x&#39;], df[&#39;y&#39;], line_width=2)<\/p>\n<p>output_file(&#39;bokeh_plot.html&#39;)<\/p>\n<p>show(p)<\/p>\n<h2><strong>\u5bfc\u51fa\u6570\u636e\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;bokeh_plot_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684Python\u5e93\u6765\u5bfc\u51fa\u56fe\u8868\u6570\u636e\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u4f7f\u7528\u3002\u65e0\u8bba\u662f\u5bfc\u51fa\u4e3aCSV\u3001Excel\u3001JSON\u8fd8\u662f\u5176\u4ed6\u683c\u5f0f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u7684\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u56fe\u8868\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u56fe\u8868\u6570\u636e\u8f7b\u677e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u6570\u636e\u5df2\u5b58\u50a8\u5728\u4e00\u4e2aPandas DataFrame\u4e2d\u3002\u4f7f\u7528<code>DataFrame.to_csv(&#39;filename.csv&#39;)<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u683c\u5f0f\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u6216\u5171\u4eab\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9bPython\u5e93\u6765\u521b\u5efa\u56fe\u8868\u5e76\u5bfc\u51fa\u6570\u636e\uff1f<\/strong><br \/>\u5e38\u89c1\u7684Python\u5e93\u5305\u62ecMatplotlib\u3001Seaborn\u548cPlotly\u7b49\u3002\u8fd9\u4e9b\u5e93\u4e0d\u4ec5\u80fd\u521b\u5efa\u89c6\u89c9\u6548\u679c\u51fa\u8272\u7684\u56fe\u8868\uff0c\u8fd8\u53ef\u4ee5\u4e0ePandas\u7ed3\u5408\uff0c\u65b9\u4fbf\u5730\u5bfc\u51fa\u6570\u636e\u3002\u901a\u8fc7\u5c06\u6570\u636e\u63d0\u53d6\u5230DataFrame\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5bfc\u51fa\u6240\u9700\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u56fe\u8868\u4e2d\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u70b9\uff1f<\/strong><br \/>\u82e5\u60f3\u4ece\u56fe\u8868\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u636e\u70b9\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u8bbf\u95ee\u6570\u636e\u6e90\u3002\u901a\u5e38\uff0c\u56fe\u8868\u7684\u7ed8\u5236\u6570\u636e\u4f1a\u5b58\u5728\u4e8e\u4e00\u4e2a\u6570\u7ec4\u6216DataFrame\u4e2d\u3002\u901a\u8fc7\u7d22\u5f15\u6216\u6761\u4ef6\u7b5b\u9009\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u7279\u5b9a\u7684\u6570\u636e\u70b9\uff0c\u5e76\u6839\u636e\u9700\u8981\u5c06\u5176\u5bfc\u51fa\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u7684\u56fe\u8868\u6570\u636e\u53ef\u4ee5\u5bfc\u51fa\u6210\u591a\u79cd\u683c\u5f0f\u3001\u5305\u62ecCSV\u3001Excel\u3001JSON\u7b49\u683c\u5f0f\u3002\u4e3a\u4e86\u8be6\u7ec6\u5bfc\u51fa\u6570\u636e\uff0c\u4e0b\u9762\u6211 [&hellip;]","protected":false},"author":3,"featured_media":1072176,"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\/1072170"}],"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=1072170"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072170\/revisions"}],"predecessor-version":[{"id":1072177,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072170\/revisions\/1072177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072176"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}