{"id":1153438,"date":"2025-01-13T17:38:47","date_gmt":"2025-01-13T09:38:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153438.html"},"modified":"2025-01-13T17:38:50","modified_gmt":"2025-01-13T09:38:50","slug":"%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153438.html","title":{"rendered":"\u753b\u56fe\u5982\u4f55\u7528python\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183205\/e057577c-5f28-4c50-96d4-9bcb3666ddc8.webp\" alt=\"\u753b\u56fe\u5982\u4f55\u7528python\u5b9e\u73b0\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u5b9e\u73b0\u753b\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\uff0c\u5982<strong>Matplotlib\u3001Seaborn\u3001Plotly<\/strong>\uff0c\u8fd9\u4e09\u4e2a\u5e93\u662f\u6700\u5e38\u7528\u7684\u3002<strong>Matplotlib<\/strong>\u662f\u4e00\u4e2a\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c<strong>Seaborn<\/strong>\u57fa\u4e8eMatplotlib\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c<strong>Plotly<\/strong>\u5219\u652f\u6301\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MATPLOTLIB<\/h3>\n<\/p>\n<p><h4>1.1 \u6982\u8ff0<\/h4>\n<\/p>\n<p><p><strong>Matplotlib<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u5176\u57fa\u672c\u5143\u7d20\u662f<code>Figure<\/code>\u548c<code>Axes<\/code>\uff0c\u4e00\u4e2a<code>Figure<\/code>\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a<code>Axes<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b89\u88c5<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u57fa\u672c\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Simple Line Plot&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\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001SEABORN<\/h3>\n<\/p>\n<p><h4>2.1 \u6982\u8ff0<\/h4>\n<\/p>\n<p><p><strong>Seaborn<\/strong>\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002\u5b83\u4f7f\u5f97\u521b\u5efa\u590d\u6742\u7684\u56fe\u8868\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u7f8e\u89c2\u3002Seaborn\u96c6\u6210\u4e86pandas\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u76f4\u63a5\u5904\u7406DataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u5b89\u88c5<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u57fa\u672c\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\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\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;x&#39;, y=&#39;y&#39;, data=df)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Seaborn Line Plot&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\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001PLOTLY<\/h3>\n<\/p>\n<p><h4>3.1 \u6982\u8ff0<\/h4>\n<\/p>\n<p><p><strong>Plotly<\/strong>\u662f\u4e00\u4e2a\u652f\u6301\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u7cbe\u7f8e\u7684\u56fe\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7f51\u9875\u4e2d\u663e\u793a\u3002Plotly\u9002\u7528\u4e8e\u9700\u8981\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u573a\u666f\uff0c\u5982\u4eea\u8868\u76d8\u3001\u6570\u636e\u5206\u6790\u5e94\u7528\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u5b89\u88c5<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u57fa\u672c\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\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<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.line(data, x=&#39;x&#39;, y=&#39;y&#39;, title=&#39;Interactive Line Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6298\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>\u6298\u7ebf\u56fe\u7528\u4e8e\u663e\u793a\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u9002\u5408\u7528\u6765\u5c55\u793a\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>4.1 Matplotlib\u6298\u7ebf\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>months = [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;, &#39;May&#39;, &#39;Jun&#39;]<\/p>\n<p>values = [10, 15, 7, 20, 25, 30]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(months, values, marker=&#39;o&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Monthly Values&quot;)<\/p>\n<p>plt.xlabel(&quot;Month&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 Seaborn\u6298\u7ebf\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;month&#39;: [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;, &#39;May&#39;, &#39;Jun&#39;],<\/p>\n<p>    &#39;value&#39;: [10, 15, 7, 20, 25, 30]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;month&#39;, y=&#39;value&#39;, data=df, marker=&#39;o&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Monthly Values&quot;)<\/p>\n<p>plt.xlabel(&quot;Month&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 Plotly\u6298\u7ebf\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;month&#39;: [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;, &#39;May&#39;, &#39;Jun&#39;],<\/p>\n<p>    &#39;value&#39;: [10, 15, 7, 20, 25, 30]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.line(data, x=&#39;month&#39;, y=&#39;value&#39;, title=&#39;Monthly Values&#39;, markers=True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u67f1\u72b6\u56fe\u7528\u4e8e\u6bd4\u8f83\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\uff0c\u9002\u5408\u7528\u6765\u5c55\u793a\u5206\u7c7b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>5.1 Matplotlib\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [4, 7, 1, 8, 5]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.bar(categories, values)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Category Values&quot;)<\/p>\n<p>plt.xlabel(&quot;Category&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 Seaborn\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;category&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;],<\/p>\n<p>    &#39;value&#39;: [4, 7, 1, 8, 5]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.barplot(x=&#39;category&#39;, y=&#39;value&#39;, data=df)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Category Values&quot;)<\/p>\n<p>plt.xlabel(&quot;Category&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3 Plotly\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;category&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;],<\/p>\n<p>    &#39;value&#39;: [4, 7, 1, 8, 5]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.bar(data, x=&#39;category&#39;, y=&#39;value&#39;, title=&#39;Category Values&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>\u6563\u70b9\u56fe\u7528\u4e8e\u663e\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u9002\u5408\u7528\u6765\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u6216\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><h4>6.1 Matplotlib\u6563\u70b9\u56fe<\/h4>\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 = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\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<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 Seaborn\u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\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\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.scatterplot(x=&#39;x&#39;, y=&#39;y&#39;, data=df)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\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<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 Plotly\u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\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<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.scatter(data, x=&#39;x&#39;, y=&#39;y&#39;, title=&#39;Scatter Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u997c\u56fe<\/h3>\n<\/p>\n<p><p>\u997c\u56fe\u7528\u4e8e\u663e\u793a\u5404\u90e8\u5206\u5728\u6574\u4f53\u4e2d\u7684\u5360\u6bd4\uff0c\u9002\u5408\u7528\u6765\u5c55\u793a\u7ec4\u6210\u90e8\u5206\u7684\u6bd4\u4f8b\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h4>7.1 Matplotlib\u997c\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&quot;Pie Chart&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 Seaborn\u997c\u56fe<\/h4>\n<\/p>\n<p><p>Seaborn\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u997c\u56fe\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6765\u7ed8\u5236\u997c\u56fe\uff0c\u7136\u540e\u4f7f\u7528Seaborn\u7684\u98ce\u683c\u3002<\/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>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u8bbe\u7f6eSeaborn\u98ce\u683c<\/strong><\/h2>\n<p>sns.set()<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&quot;Pie Chart&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.3 Plotly\u997c\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;labels&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;],<\/p>\n<p>    &#39;sizes&#39;: [15, 30, 45, 10]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.pie(data, values=&#39;sizes&#39;, names=&#39;labels&#39;, title=&#39;Pie Chart&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u76f4\u65b9\u56fe\u7528\u4e8e\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u9002\u5408\u7528\u6765\u67e5\u770b\u6570\u636e\u7684\u9891\u7387\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><h4>8.1 Matplotlib\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.hist(data, bins=5)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Value&quot;)<\/p>\n<p>plt.ylabel(&quot;Frequency&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 Seaborn\u76f4\u65b9\u56fe<\/h4>\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>\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.histplot(data, bins=5)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Histogram&quot;)<\/p>\n<p>plt.xlabel(&quot;Value&quot;)<\/p>\n<p>plt.ylabel(&quot;Frequency&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.3 Plotly\u76f4\u65b9\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;values&#39;: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.histogram(data, x=&#39;values&#39;, nbins=5, title=&#39;Histogram&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u7bb1\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>\u7bb1\u7ebf\u56fe\u7528\u4e8e\u663e\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u5305\u62ec\u6570\u636e\u7684\u4e2d\u4f4d\u6570\u3001\u56db\u5206\u4f4d\u6570\u548c\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><h4>9.1 Matplotlib\u7bb1\u7ebf\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.boxplot(data)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Box Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;Data&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.2 Seaborn\u7bb1\u7ebf\u56fe<\/h4>\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>\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.boxplot(data=data)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&quot;Box Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;Data&quot;)<\/p>\n<p>plt.ylabel(&quot;Value&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.3 Plotly\u7bb1\u7ebf\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;values&#39;: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.box(data, y=&#39;values&#39;, title=&#39;Box Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u70ed\u529b\u56fe<\/h3>\n<\/p>\n<p><p>\u70ed\u529b\u56fe\u7528\u4e8e\u663e\u793a\u77e9\u9635\u6570\u636e\u7684\u5f3a\u5ea6\uff0c\u9002\u5408\u7528\u6765\u5c55\u793a\u76f8\u5173\u6027\u77e9\u9635\u6216\u9891\u7387\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><h4>10.1 Matplotlib\u70ed\u529b\u56fe<\/h4>\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>\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.imshow(data, cmap=&#39;hot&#39;, interpolation=&#39;nearest&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&quot;Heatmap&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>10.2 Seaborn\u70ed\u529b\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.heatmap(data, annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&quot;Heatmap&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>10.3 Plotly\u70ed\u529b\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.imshow(data, text_auto=True, aspect=&#39;auto&#39;, title=&#39;Heatmap&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528<strong>Matplotlib\u3001Seaborn\u3001Plotly<\/strong>\u8fd9\u4e09\u4e2a\u5e93\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u8868\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u3001\u997c\u56fe\u3001\u76f4\u65b9\u56fe\u3001\u7bb1\u7ebf\u56fe\u548c\u70ed\u529b\u56fe\u3002\u6bcf\u79cd\u56fe\u8868\u5747\u63d0\u4f9b\u4e86\u4e09\u4e2a\u5e93\u7684\u5b9e\u73b0\u4ee3\u7801\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5e93\u7684\u7279\u70b9\u8fdb\u884c\u4e86\u7b80\u5355\u63cf\u8ff0\u3002\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u8bfb\u8005\u53ef\u4ee5\u4e86\u89e3\u5230\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4ece\u800c\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u5e93\u3002\u4f8b\u5982\uff0c<strong>Matplotlib<\/strong>\u9002\u5408\u57fa\u672c\u7ed8\u56fe\uff0c<strong>Seaborn<\/strong>\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\uff0c<strong>Plotly<\/strong>\u9002\u5408\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u56fe\u5f62\uff0c\u6700\u5e38\u7528\u7684\u662fMatplotlib\u3002\u901a\u8fc7\u5b89\u88c5Matplotlib\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u548c\u67f1\u72b6\u56fe\u3002\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c<code>plt.scatter()<\/code>\u7528\u4e8e\u6563\u70b9\u56fe\uff0c<code>plt.bar()<\/code>\u5219\u9002\u5408\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u5b89\u88c5\u540e\u53ea\u9700\u7b80\u5355\u7684\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u7ed8\u56fe\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u56fe\u5f62\u7ed8\u5236\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u53ef\u7528\u4e8e\u56fe\u5f62\u7ed8\u5236\u3002Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u9002\u5408\u8fdb\u884c\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\u3002Plotly\u5219\u53ef\u4ee5\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u9002\u5408\u5728\u7f51\u9875\u4e0a\u5c55\u793a\u3002\u6b64\u5916\uff0cPIL\uff08Pillow\uff09\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u5904\u7406\u56fe\u50cf\uff0c\u9002\u5408\u66f4\u590d\u6742\u7684\u56fe\u5f62\u5904\u7406\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u7ed8\u5236\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u7ed8\u5236\u5b8c\u56fe\u5f62\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.savefig()<\/code>\u51fd\u6570\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u591a\u79cd\u683c\u5f0f\uff0c\u5982PNG\u3001JPEG\u6216PDF\u3002\u53ea\u9700\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff0c\u56fe\u5f62\u5c31\u4f1a\u88ab\u4fdd\u5b58\u5728\u6307\u5b9a\u8def\u5f84\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u4e4b\u524d\u8c03\u7528<code>plt.show()<\/code>\u4ee5\u786e\u4fdd\u56fe\u5f62\u5185\u5bb9\u5b8c\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5b9e\u73b0\u753b\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u3001Plotly\uff0c\u8fd9\u4e09\u4e2a\u5e93\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1153447,"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\/1153438"}],"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=1153438"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153438\/revisions"}],"predecessor-version":[{"id":1153450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153438\/revisions\/1153450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153447"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}