{"id":1168401,"date":"2025-01-15T15:57:32","date_gmt":"2025-01-15T07:57:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168401.html"},"modified":"2025-01-15T15:57:34","modified_gmt":"2025-01-15T07:57:34","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%98%e5%88%b6%e5%87%bd%e6%95%b0%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168401.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7ed8\u5236\u51fd\u6570\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065640\/4be9854f-10b6-40f7-ae3c-8868fac71ad0.webp\" alt=\"python\u4e2d\u5982\u4f55\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528Matplotlib\u5e93\u3001Seaborn\u5e93\u3001Plotly\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Matplotlib\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u3001\u6700\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c\u5b89\u88c5Matplotlib\u5e93<\/strong>\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4 <code>pip install matplotlib<\/code> \u6765\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u6570\u636e\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>plot()<\/code> \u65b9\u6cd5\u7ed8\u5236\u56fe\u50cf\uff0c\u6700\u540e\u4f7f\u7528 <code>show()<\/code> \u65b9\u6cd5\u5c55\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u6570\u636e\u65f6\uff0c\u4f7f\u7528 <code>numpy<\/code> \u5e93\u751f\u6210\u6570\u636e\u70b9\u3002\u4f8b\u5982\uff0c<code>np.linspace(-10, 10, 100)<\/code> \u521b\u5efa\u4e86\u4ece-10\u523010\u7684100\u4e2a\u5747\u5300\u5206\u5e03\u7684\u6570\u636e\u70b9\u3002\u8fd9\u4e9b\u6570\u636e\u70b9\u901a\u8fc7 <code>numpy<\/code> \u7684\u6570\u5b66\u51fd\u6570\uff08\u5982 <code>np.sin(x)<\/code>\uff09\u751f\u6210\u5bf9\u5e94\u7684y\u503c\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>plt.plot(x, y)<\/code> \u7ed8\u5236\u56fe\u50cf\uff0c<code>plt.show()<\/code> \u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001MATPLOTLIB\u5e93<\/h3>\n<\/p>\n<p><p><strong>Matplotlib<\/strong> \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684\u7efc\u5408\u5e93\u3002\u5b83\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u5de5\u7a0b\u9886\u57df\u3002\u5176\u57fa\u672c\u7528\u6cd5\u6d89\u53ca\u5bfc\u5165\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u7ed8\u5236\u56fe\u50cf\u548c\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u5728\u57fa\u672c\u7ed8\u56fe\u4e2d\uff0c\u6211\u4eec\u4e3b\u8981\u5173\u6ce8\u5982\u4f55\u7528Matplotlib\u5e93\u7ed8\u5236\u7b80\u5355\u7684\u4e8c\u7ef4\u56fe\u50cf\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Sine Wave&quot;)<\/p>\n<p>plt.xlabel(&quot;X Axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y Axis&quot;)<\/p>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4ece-10\u523010\u7684100\u4e2a\u6570\u636e\u70b9\uff0c\u7136\u540e\u8ba1\u7b97\u8fd9\u4e9b\u70b9\u7684\u6b63\u5f26\u503c\u3002\u4f7f\u7528 <code>plt.plot(x, y)<\/code> \u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u5e76\u6dfb\u52a0\u4e86\u6807\u9898\u548c\u8f74\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><p>\u6563\u70b9\u56fe\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u56fe\u5f62\uff0c\u7528\u4e8e\u663e\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.random.rand(50)<\/p>\n<p>y = np.random.rand(50)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y, c=&#39;red&#39;, alpha=0.5)<\/p>\n<p>plt.title(&quot;Scatter Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X Axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y Axis&quot;)<\/p>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>np.random.rand(50)<\/code> \u751f\u621050\u4e2a\u968f\u673a\u6570\uff0c\u7136\u540e\u4f7f\u7528 <code>plt.scatter(x, y)<\/code> \u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u8bbe\u7f6e\u70b9\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><p>\u67f1\u72b6\u56fe\u7528\u4e8e\u6bd4\u8f83\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>values = [10, 15, 7, 12]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.bar(categories, values, color=&#39;blue&#39;, alpha=0.7)<\/p>\n<p>plt.title(&quot;Bar Chart&quot;)<\/p>\n<p>plt.xlabel(&quot;Categories&quot;)<\/p>\n<p>plt.ylabel(&quot;Values&quot;)<\/p>\n<p>plt.grid(True, axis=&#39;y&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>plt.bar(categories, values)<\/code> \u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6e\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001SEABORN\u5e93<\/h3>\n<\/p>\n<p><p><strong>Seaborn<\/strong> \u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u7ed8\u5236\u5438\u5f15\u4eba\u7684\u548c\u4fe1\u606f\u4e30\u5bcc\u7684\u7edf\u8ba1\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Seaborn\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4 <code>pip install seaborn<\/code> \u6765\u5b89\u88c5\u3002\u7136\u540e\uff0c\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.normal(size=100)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>sns.histplot(data, kde=True)<\/p>\n<p>plt.title(&quot;Histogram with KDE&quot;)<\/p>\n<p>plt.xlabel(&quot;Value&quot;)<\/p>\n<p>plt.ylabel(&quot;Frequency&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>sns.histplot(data, kde=True)<\/code> \u7ed8\u5236\u76f4\u65b9\u56fe\uff0c\u5e76\u6dfb\u52a0\u6838\u5bc6\u5ea6\u4f30\u8ba1\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u70ed\u529b\u56fe<\/h4>\n<\/p>\n<p><p>\u70ed\u529b\u56fe\u7528\u4e8e\u663e\u793a\u6570\u636e\u77e9\u9635\u7684\u70ed\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>data = np.random.rand(10, 12)<\/p>\n<h2><strong>\u7ed8\u5236\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(data, annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.title(&quot;Heatmap&quot;)<\/p>\n<p>plt.xlabel(&quot;X Axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y Axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>sns.heatmap(data, annot=True, cmap=&#39;coolwarm&#39;)<\/code> \u7ed8\u5236\u70ed\u529b\u56fe\uff0c\u5e76\u6dfb\u52a0\u6ce8\u91ca\u548c\u989c\u8272\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001PLOTLY\u5e93<\/h3>\n<\/p>\n<p><p><strong>Plotly<\/strong> \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u5f62\u7684\u5e93\uff0c\u9002\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301\u591a\u79cd\u56fe\u8868\u7c7b\u578b\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Plotly\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4 <code>pip install plotly<\/code> \u6765\u5b89\u88c5\u3002\u7136\u540e\uff0c\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<p>fig.add_trace(go.Scatter(x=x, y=y, mode=&#39;lines&#39;, name=&#39;Sine Wave&#39;))<\/p>\n<p>fig.update_layout(title=&#39;Sine Wave&#39;, xaxis_title=&#39;X Axis&#39;, yaxis_title=&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>go.Figure()<\/code> \u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528 <code>go.Scatter()<\/code> \u6dfb\u52a0\u6570\u636e\u548c\u7ed8\u56fe\u6a21\u5f0f\u3002\u6700\u540e\uff0c\u4f7f\u7528 <code>fig.show()<\/code> \u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>3.2 3D\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>Plotly\u8fd8\u652f\u63013D\u56fe\u5f62\uff0c\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4e09\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.linspace(-10, 10, 100)<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y[:, np.newaxis]&lt;\/strong&gt;2))<\/p>\n<h2><strong>\u7ed8\u52363D\u8868\u9762\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])<\/p>\n<p>fig.update_layout(title=&#39;3D Surface Plot&#39;, scene=dict(<\/p>\n<p>                    xaxis_title=&#39;X Axis&#39;,<\/p>\n<p>                    yaxis_title=&#39;Y Axis&#39;,<\/p>\n<p>                    zaxis_title=&#39;Z Axis&#39;))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>go.Surface()<\/code> \u6dfb\u52a0\u6570\u636e\u5e76\u7ed8\u52363D\u8868\u9762\u56fe\u3002\u53ef\u4ee5\u901a\u8fc7 <code>fig.update_layout()<\/code> \u8bbe\u7f6e\u8f74\u6807\u7b7e\u548c\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001PANDAS\u5e93<\/h3>\n<\/p>\n<p><p><strong>Pandas<\/strong> \u5e93\u4e0d\u4ec5\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\u65f6\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\u63a5\u53e3\uff0c\u53ef\u4ee5\u76f4\u63a5\u4eceDataFrame\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: np.linspace(-10, 10, 100),<\/p>\n<p>    &#39;y&#39;: np.sin(np.linspace(-10, 10, 100))<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df.plot(x=&#39;x&#39;, y=&#39;y&#39;, kind=&#39;line&#39;, title=&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&quot;X Axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y Axis&quot;)<\/p>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>df.plot()<\/code> \u76f4\u63a5\u7ed8\u5236DataFrame\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u6307\u5b9a\u7ed8\u56fe\u7c7b\u578b\u4e3a\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u591a\u4e2a\u5b50\u56fe<\/h4>\n<\/p>\n<p><p>Pandas\u8fd8\u652f\u6301\u5728\u540c\u4e00\u4e2a\u56fe\u5f62\u4e2d\u7ed8\u5236\u591a\u4e2a\u5b50\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: np.linspace(-10, 10, 100),<\/p>\n<p>    &#39;y1&#39;: np.sin(np.linspace(-10, 10, 100)),<\/p>\n<p>    &#39;y2&#39;: np.cos(np.linspace(-10, 10, 100))<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u591a\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>df.plot(subplots=True, layout=(2, 1), figsize=(6, 8), title=&#39;Sine and Cosine Waves&#39;)<\/p>\n<p>plt.xlabel(&quot;X Axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y Axis&quot;)<\/p>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>df.plot(subplots=True)<\/code> \u7ed8\u5236\u591a\u4e2a\u5b50\u56fe\uff0c\u5e76\u901a\u8fc7 <code>layout<\/code> \u53c2\u6570\u6307\u5b9a\u5b50\u56fe\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u7ed8\u56fe\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u5916\uff0cPython\u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7ed8\u56fe\u5e93\uff0c\u5982Bokeh\u3001Alt<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r\u3001ggplot\u7b49\uff0c\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u7279\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>5.1 Bokeh\u5e93<\/h4>\n<\/p>\n<p><p><strong>Bokeh<\/strong> \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u548c\u53ef\u6269\u5c55\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u9002\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u3002<\/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>import numpy as np<\/p>\n<h2><strong>\u8f93\u51fa\u5230notebook<\/strong><\/h2>\n<p>output_notebook()<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>p = figure(title=&quot;Sine Wave&quot;, x_axis_label=&#39;X Axis&#39;, y_axis_label=&#39;Y Axis&#39;)<\/p>\n<p>p.line(x, y, legend_label=&quot;Sine&quot;, line_width=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>figure()<\/code> \u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528 <code>p.line()<\/code> \u6dfb\u52a0\u6570\u636e\u5e76\u7ed8\u5236\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h4>5.2 Altair\u5e93<\/h4>\n<\/p>\n<p><p><strong>Altair<\/strong> \u662f\u4e00\u4e2a\u58f0\u660e\u5f0f\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u57fa\u4e8eVega\u548cVega-Lite\u6784\u5efa\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u590d\u6742\u7684\u56fe\u8868\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>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>data = pd.DataFrame({&#39;x&#39;: x, &#39;y&#39;: y})<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>chart = alt.Chart(data).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;Sine Wave&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>chart.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>alt.Chart(data).mark_line().encode()<\/code> \u521b\u5efa\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6e\u6570\u636e\u6620\u5c04\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001Seaborn\u5e93\u3001Plotly\u5e93\u7b49\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u7279\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>Matplotlib<\/strong> \u662f\u6700\u5e38\u7528\u3001\u6700\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u50cf\u3002<strong>Seaborn<\/strong> \u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u548c\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\uff0c\u9002\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\u3002<strong>Plotly<\/strong> \u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u9002\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u3002<strong>Pandas<\/strong> \u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\u63a5\u53e3\uff0c\u9002\u5408\u4eceDataFrame\u7ed8\u5236\u56fe\u5f62\u3002<strong>Bokeh<\/strong> \u548c <strong>Altair<\/strong> \u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u548c\u590d\u6742\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u548c\u5206\u6790\u7ed3\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6570\u5b66\u51fd\u6570\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u6570\u5b66\u51fd\u6570\u56fe\u50cf\u901a\u5e38\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install matplotlib<\/code>\u547d\u4ee4\u3002\u63a5\u4e0b\u6765\uff0c\u5bfc\u5165\u5e93\u5e76\u5b9a\u4e49\u8981\u7ed8\u5236\u7684\u51fd\u6570\u3002\u901a\u8fc7<code>numpy<\/code>\u751f\u6210\u4e00\u7cfb\u5217\u7684x\u503c\uff0c\u7136\u540e\u8ba1\u7b97\u5bf9\u5e94\u7684y\u503c\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u7ed8\u5236\u56fe\u50cf\u5e76\u8c03\u7528<code>plt.show()<\/code>\u663e\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>matplotlib<\/code>\u548c<code>numpy<\/code>\u3002<code>matplotlib<\/code>\u8d1f\u8d23\u56fe\u5f62\u7684\u7ed8\u5236\uff0c\u800c<code>numpy<\/code>\u5219\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u7279\u522b\u662f\u5728\u751f\u6210\u6570\u636e\u70b9\u65f6\u3002\u786e\u4fdd\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5b83\u4eec\uff0c\u4ee5\u4fbf\u987a\u5229\u8fdb\u884c\u56fe\u50cf\u7ed8\u5236\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u7ed8\u5236\u7684\u51fd\u6570\u56fe\u50cf\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528<code>matplotlib<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u81ea\u5b9a\u4e49\u56fe\u50cf\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\u3001\u7ebf\u578b\u3001\u6807\u8bb0\u6837\u5f0f\u4ee5\u53ca\u6807\u7b7e\u7b49\u3002\u901a\u8fc7<code>plt.plot(x, y, color=&#39;red&#39;, linestyle=&#39;--&#39;, marker=&#39;o&#39;)<\/code>\u53ef\u4ee5\u6539\u53d8\u7ebf\u6761\u7684\u989c\u8272\u3001\u7c7b\u578b\u548c\u6807\u8bb0\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>plt.title()<\/code>\u3001<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u6dfb\u52a0\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\uff0c\u63d0\u5347\u56fe\u50cf\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u5236\u51fd\u6570\u56fe\u50cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528Matplotlib\u5e93\u3001Seaborn\u5e93\u3001Plotl [&hellip;]","protected":false},"author":3,"featured_media":1168407,"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\/1168401"}],"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=1168401"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168401\/revisions"}],"predecessor-version":[{"id":1168409,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168401\/revisions\/1168409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168407"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}