{"id":1149220,"date":"2025-01-13T16:46:57","date_gmt":"2025-01-13T08:46:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149220.html"},"modified":"2025-01-13T16:47:00","modified_gmt":"2025-01-13T08:47:00","slug":"python%e7%bb%98%e5%9b%be%e7%aa%97%e5%8f%a3%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149220.html","title":{"rendered":"Python\u7ed8\u56fe\u7a97\u53e3\u5982\u4f55\u663e\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25175907\/0a6e32ba-7969-4e92-8e21-3fe2f051c6ce.webp\" alt=\"Python\u7ed8\u56fe\u7a97\u53e3\u5982\u4f55\u663e\u793a\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7ed8\u56fe\u5e93\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ecMatplotlib\u3001Seaborn\u548cPlotly\u7b49\u3002<strong>Matplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u3001\u652f\u6301\u591a\u79cd\u7ed8\u56fe\u7c7b\u578b\u3001\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0eNumPy\u548cPandas\u914d\u5408\u4f7f\u7528<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u6765\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MATPLOTLIB\u7ed8\u56fe\u7a97\u53e3\u663e\u793a<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u4e0b\u9762\u662f\u4f7f\u7528Matplotlib\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 20, 25, 30, 35]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Simple Line Plot&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u8981\u7ed8\u5236\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>plt.plot()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>3. \u663e\u793a\u591a\u4e2a\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e00\u4e2a\u7a97\u53e3\u4e2d\u663e\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528subplot\u529f\u80fd\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [10, 20, 25, 30, 35]<\/p>\n<p>y2 = [15, 25, 30, 35, 40]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7b2c\u4e00\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>plt.subplot(2, 1, 1)<\/p>\n<p>plt.plot(x, y1)<\/p>\n<p>plt.title(&#39;First Subplot&#39;)<\/p>\n<h2><strong>\u7b2c\u4e8c\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>plt.subplot(2, 1, 2)<\/p>\n<p>plt.plot(x, y2)<\/p>\n<p>plt.title(&#39;Second Subplot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>plt.subplot(2, 1, 1)<\/code>\u8868\u793a\u4e00\u4e2a2\u884c1\u5217\u7684\u5b50\u56fe\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b50\u56fe\uff0c<code>plt.subplot(2, 1, 2)<\/code>\u8868\u793a\u7b2c\u4e8c\u4e2a\u5b50\u56fe\u3002\u6700\u540e\u4f7f\u7528<code>plt.tight_layout()<\/code>\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u786e\u4fdd\u56fe\u5f62\u4e0d\u4f1a\u91cd\u53e0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001SEABORN\u7ed8\u56fe\u7a97\u53e3\u663e\u793a<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u3001\u66f4\u6613\u7528\u7684\u63a5\u53e3\u3002\u5b83\u7279\u522b\u9002\u5408\u7ed8\u5236\u7edf\u8ba1\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Seaborn<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Seaborn\u7ed8\u5236\u4e00\u4e2a\u6563\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>tips = sns.load_dataset(&quot;tips&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(data=tips, x=&quot;total_bill&quot;, y=&quot;tip&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Seaborn\u5e93\uff0c\u7136\u540e\u52a0\u8f7d\u4e86\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\u201ctips\u201d\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>sns.scatterplot()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>3. \u663e\u793a\u591a\u4e2a\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>Seaborn\u540c\u6837\u652f\u6301\u663e\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528FacetGrid\u529f\u80fd\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>tips = sns.load_dataset(&quot;tips&quot;)<\/p>\n<h2><strong>\u521b\u5efaFacetGrid\u5bf9\u8c61<\/strong><\/h2>\n<p>g = sns.FacetGrid(tips, col=&quot;time&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>g.map(sns.scatterplot, &quot;total_bill&quot;, &quot;tip&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>sns.FacetGrid()<\/code>\u521b\u5efa\u4e86\u4e00\u4e2aFacetGrid\u5bf9\u8c61\uff0c\u901a\u8fc7<code>g.map()<\/code>\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5b50\u56fe\u4e2d\u7ed8\u5236\u6563\u70b9\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001PLOTLY\u7ed8\u56fe\u7a97\u53e3\u663e\u793a<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u7ed8\u56fe\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8eWeb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Plotly<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Plotly\u7ed8\u5236\u4e00\u4e2a\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>df = px.data.gapminder().query(&quot;country == &#39;Canada&#39;&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = px.line(df, x=&quot;year&quot;, y=&quot;gdpPercap&quot;, title=&#39;GDP per Capita of Canada&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Plotly\u5e93\u4e2d\u7684express\u6a21\u5757\uff0c\u7136\u540e\u52a0\u8f7d\u4e86\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\u201cgapminder\u201d\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>px.line()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>fig.show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>3. \u663e\u793a\u591a\u4e2a\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>Plotly\u540c\u6837\u652f\u6301\u663e\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528subplots\u6a21\u5757\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from plotly.subplots import make_subplots<\/p>\n<p>import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = make_subplots(rows=2, cols=1)<\/p>\n<h2><strong>\u6dfb\u52a0\u7b2c\u4e00\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.add_trace(<\/p>\n<p>    go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13]),<\/p>\n<p>    row=1, col=1<\/p>\n<p>)<\/p>\n<h2><strong>\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.add_trace(<\/p>\n<p>    go.Scatter(x=[1, 2, 3, 4], y=[20, 21, 22, 23]),<\/p>\n<p>    row=2, col=1<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>make_subplots()<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u56fe\u5bf9\u8c61\uff0c\u901a\u8fc7<code>fig.add_trace()<\/code>\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5b50\u56fe\u4e2d\u6dfb\u52a0\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001PANDAS\u7ed8\u56fe\u7a97\u53e3\u663e\u793a<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u5185\u7f6e\u4e86\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u7ed8\u5236\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pandas\u7ed8\u5236\u4e00\u4e2a\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;year&#39;: [2010, 2011, 2012, 2013, 2014],<\/p>\n<p>    &#39;gdp&#39;: [300, 350, 400, 450, 500]<\/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;year&#39;, y=&#39;gdp&#39;, kind=&#39;line&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Pandas\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548cGDP\u7684\u6570\u636e\u6846\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>df.plot()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>3. \u663e\u793a\u591a\u4e2a\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>Pandas\u540c\u6837\u652f\u6301\u663e\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528subplot\u53c2\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data1 = {<\/p>\n<p>    &#39;year&#39;: [2010, 2011, 2012, 2013, 2014],<\/p>\n<p>    &#39;gdp&#39;: [300, 350, 400, 450, 500]<\/p>\n<p>}<\/p>\n<p>data2 = {<\/p>\n<p>    &#39;year&#39;: [2010, 2011, 2012, 2013, 2014],<\/p>\n<p>    &#39;pop&#39;: [30, 35, 40, 45, 50]<\/p>\n<p>}<\/p>\n<p>df1 = pd.DataFrame(data1)<\/p>\n<p>df2 = pd.DataFrame(data2)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots(2, 1)<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e00\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>df1.plot(x=&#39;year&#39;, y=&#39;gdp&#39;, kind=&#39;line&#39;, ax=ax[0])<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e8c\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>df2.plot(x=&#39;year&#39;, y=&#39;pop&#39;, kind=&#39;line&#39;, ax=ax[1])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e24\u4e2a\u6570\u636e\u6846\uff0c\u7136\u540e\u901a\u8fc7<code>fig, ax = plt.subplots(2, 1)<\/code>\u521b\u5efa\u4e86\u5305\u542b\u4e24\u4e2a\u5b50\u56fe\u7684\u56fe\u5f62\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>df1.plot()<\/code>\u548c<code>df2.plot()<\/code>\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5b50\u56fe\u4e2d\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>plt.tight_layout()<\/code>\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u786e\u4fdd\u56fe\u5f62\u4e0d\u4f1a\u91cd\u53e0\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\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Bokeh\u3001Alt<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r\u7b49\uff0c\u5b83\u4eec\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. Bokeh<\/h4>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u7ed8\u56fe\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8eWeb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h5>\u5b89\u88c5Bokeh<\/h5>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install bokeh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u57fa\u672c\u7ed8\u56fe<\/h5>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Bokeh\u7ed8\u5236\u4e00\u4e2a\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show, output_file<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>p = figure(title=&quot;Simple Line Plot&quot;, x_axis_label=&#39;x&#39;, y_axis_label=&#39;y&#39;)<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [6, 7, 2, 4, 5]<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>p.line(x, y, legend_label=&quot;Temp.&quot;, line_width=2)<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file(&quot;line.html&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Bokeh\u5e93\u4e2d\u7684\u5fc5\u8981\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>p.line()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>2. Altair<\/h4>\n<\/p>\n<p><p>Altair\u662f\u4e00\u4e2a\u58f0\u660e\u5f0f\u7edf\u8ba1\u53ef\u89c6\u5316\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h5>\u5b89\u88c5Altair<\/h5>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install altair<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u57fa\u672c\u7ed8\u56fe<\/h5>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Altair\u7ed8\u5236\u4e00\u4e2a\u6563\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import altair as alt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [6, 7, 2, 4, 5]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>chart = alt.Chart(data).mark_point().encode(<\/p>\n<p>    x=&#39;x&#39;,<\/p>\n<p>    y=&#39;y&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>chart.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Altair\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542bx\u548cy\u6570\u636e\u7684\u6570\u636e\u6846\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>alt.Chart(data).mark_point().encode()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u6700\u540e\u4f7f\u7528<code>chart.show()<\/code>\u65b9\u6cd5\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u7ed8\u56fe\u5e93\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<strong>Matplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93<\/strong>\uff0c\u652f\u6301\u591a\u79cd\u7ed8\u56fe\u7c7b\u578b\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0eNumPy\u548cPandas\u914d\u5408\u4f7f\u7528\u3002<strong>Seaborn\u57fa\u4e8eMatplotlib\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u3001\u66f4\u6613\u7528\u7684\u63a5\u53e3<\/strong>\uff0c\u7279\u522b\u9002\u5408\u7ed8\u5236\u7edf\u8ba1\u56fe\u8868\u3002<strong>Plotly\u548cBokeh\u5219\u9002\u5408\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868<\/strong>\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8eWeb\u5e94\u7528\u3002<strong>Altair\u662f\u4e00\u4e2a\u58f0\u660e\u5f0f\u7edf\u8ba1\u53ef\u89c6\u5316\u5e93<\/strong>\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u7ed8\u56fe\u5e93\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4e30\u5bcc\u7684\u6587\u6863\u548c\u793a\u4f8b\u6765\u5feb\u901f\u4e0a\u624b\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u7ed8\u56fe\u5e93\u6765\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u521b\u5efa\u7ed8\u56fe\u7a97\u53e3\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u3002\u901a\u8fc7\u5bfc\u5165Matplotlib\u5e93\u5e76\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import matplotlib.pyplot as plt<\/code>\u6765\u5bfc\u5165\u5e93\uff0c\u7136\u540e\u7ed8\u5236\u56fe\u5f62\u5e76\u8c03\u7528<code>plt.show()<\/code>\u5373\u53ef\u663e\u793a\u7a97\u53e3\u3002<\/p>\n<p><strong>\u7ed8\u56fe\u7a97\u53e3\u4e0d\u663e\u793a\u7684\u5e38\u89c1\u539f\u56e0\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u7ed8\u56fe\u7a97\u53e3\u6ca1\u6709\u6b63\u5e38\u663e\u793a\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u591a\u4e2a\u539f\u56e0\u9020\u6210\u7684\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5728\u4ee3\u7801\u7684\u6700\u540e\u8c03\u7528\u4e86<code>plt.show()<\/code>\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u662f\u5426\u5728\u5408\u9002\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\uff0c\u4f8b\u5982\u67d0\u4e9bIDE\u6216Jupyter Notebook\u53ef\u80fd\u9700\u8981\u7279\u6b8a\u914d\u7f6e\u4ee5\u663e\u793a\u7a97\u53e3\u3002\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u5e76\u4e14\u6ca1\u6709\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ed8\u56fe\u7a97\u53e3\u4e2d\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u7ed8\u56fe\u7a97\u53e3\u4e2d\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\u53ef\u4ee5\u4f7f\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\u3002\u4f7f\u7528<code>plt.title(&#39;\u4f60\u7684\u6807\u9898&#39;)<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u5f62\u7684\u6807\u9898\uff0c\u4f7f\u7528<code>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/code>\u548c<code>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/code>\u53ef\u4ee5\u5206\u522b\u4e3aX\u8f74\u548cY\u8f74\u6dfb\u52a0\u6807\u7b7e\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u56fe\u8868\u6240\u4f20\u8fbe\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7ed8\u56fe\u5e93\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ecMatplotlib\u3001Seaborn\u548cPlot [&hellip;]","protected":false},"author":3,"featured_media":1149225,"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\/1149220"}],"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=1149220"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149220\/revisions"}],"predecessor-version":[{"id":1149226,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149220\/revisions\/1149226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149225"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}