{"id":176234,"date":"2024-05-08T19:06:59","date_gmt":"2024-05-08T11:06:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176234.html"},"modified":"2024-05-08T19:07:03","modified_gmt":"2024-05-08T11:07:03","slug":"python%e6%95%a3%e7%82%b9%e5%9b%be%e6%80%8e%e4%b9%88%e5%88%86%e7%b1%bb%e7%9d%80%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176234.html","title":{"rendered":"Python\u6563\u70b9\u56fe\u600e\u4e48\u5206\u7c7b\u7740\u8272"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27055802\/b8dffbf4-5b2f-410e-985e-e3cfee7c6ced.webp\" alt=\"Python\u6563\u70b9\u56fe\u600e\u4e48\u5206\u7c7b\u7740\u8272\" \/><\/p>\n<p><p>Python\u6563\u70b9\u56fe\u5206\u7c7b\u7740\u8272\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u6216seaborn\u7b49\u5e93\u6765\u5b9e\u73b0\uff0c\u5173\u952e\u5728\u4e8e<strong>\u58f0\u660e\u989c\u8272\u6620\u5c04\u3001\u5229\u7528\u5206\u7c7b\u53d8\u91cf\u8fdb\u884c\u5206\u7ec4<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u60a8\u9700\u8981\u5c06\u6570\u636e\u5206\u6210\u4e0d\u540c\u7684\u7ec4\uff0c\u5e76\u4e3a\u6bcf\u7ec4\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\u3002\u8be5\u8fc7\u7a0b\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u56fe\u8868\u7684\u89c6\u89c9\u5438\u5f15\u529b\uff0c\u800c\u4e14\u80fd\u591f\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u5404\u4e2a\u6570\u636e\u70b9\u4e4b\u95f4\u7684\u5206\u7c7b\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684scatter\u51fd\u6570\uff0c\u5e76\u901a\u8fc7c\u53c2\u6570\u4f20\u9012\u989c\u8272\u4fe1\u606f\u3002\u989c\u8272\u4fe1\u606f\u53ef\u4ee5\u662f\u6570\u636e\u96c6\u4e2d\u8868\u793a\u7c7b\u522b\u7684\u5217\uff0c\u5176\u4e2d\u4e0d\u540c\u7684\u503c\u5c06\u81ea\u52a8\u88ab\u6620\u5c04\u5230\u4e0d\u540c\u7684\u989c\u8272\u4e0a\u3002\u8fdb\u4e00\u6b65\u5730\uff0c\u4f7f\u7528Colormap\uff08cmap\uff09\u53ef\u4ee5\u81ea\u5b9a\u4e49\u989c\u8272\u8303\u56f4\uff0c\u4f7f\u5206\u7c7b\u66f4\u52a0\u76f4\u89c2\u660e\u663e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u5728\u5206\u7c7b\u7740\u8272\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u96c6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u67d0\u57ce\u5e02\u4e0d\u540c\u533a\u57df\u623f\u4ef7\u3001\u9762\u79ef\u4ee5\u53ca\u533a\u57df\u7f16\u53f7\u7684\u6570\u636e\u96c6\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u901a\u8fc7\u6563\u70b9\u56fe\u663e\u793a\u4e0d\u540c\u533a\u57df\u7684\u623f\u4ef7\u4e0e\u9762\u79ef\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u901a\u8fc7\u989c\u8272\u533a\u5206\u4e0d\u540c\u7684\u533a\u57df\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>\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#039;Area&#039;: np.random.rand(100) * 100,<\/p>\n<p>    &#039;Price&#039;: np.random.rand(100) * 500,<\/p>\n<p>    &#039;Region&#039;: np.random.randint(1, 5, size=100)<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u5206\u7c7b\u7740\u8272<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u63a5\u53e3\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5206\u7c7b\u7740\u8272<\/strong><\/h2>\n<p>colors = {1: &#039;red&#039;, 2: &#039;green&#039;, 3: &#039;blue&#039;, 4: &#039;yellow&#039;}<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>for region in df[&#039;Region&#039;].unique():<\/p>\n<p>    # \u9009\u62e9\u5f53\u524d\u533a\u57df\u7684\u6570\u636e<\/p>\n<p>    current_data = df[df[&#039;Region&#039;] == region]<\/p>\n<p>    plt.scatter(current_data[&#039;Area&#039;], current_data[&#039;Price&#039;], c=colors[region], label=f&#039;Region {region}&#039;)<\/p>\n<p>plt.title(&#039;House Price Distribution by Region&#039;)<\/p>\n<p>plt.xlabel(&#039;Area&#039;)<\/p>\n<p>plt.ylabel(&#039;Price&#039;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u901a\u8fc7\u4e3a\u4e0d\u540c\u7684\u2018Region\u2019\u503c\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\uff0c\u8fdb\u800c\u5728\u6563\u70b9\u56fe\u4e2d\u5b9e\u73b0\u4e86\u5206\u7c7b\u7740\u8272\u7684\u76ee\u7684\u3002<code>plt.legend()<\/code>\u51fd\u6570\u5728\u56fe\u8868\u4e2d\u6dfb\u52a0\u4e86\u56fe\u4f8b\uff0c\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u4e86\u989c\u8272\u4e0e\u533a\u57df\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u5206\u7c7b\u7740\u8272<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8ematplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7ed8\u56fe\u7c7b\u578b\u548c\u7f8e\u5316\u529f\u80fd\u3002\u7528Seaborn\u8fdb\u884c\u5206\u7c7b\u7740\u8272\u540c\u6837\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7684\u6563\u70b9\u56fe\u51fd\u6570\u5e76\u6307\u5b9a\u5206\u7c7b\u989c\u8272<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>sns.scatterplot(data=df, x=&#039;Area&#039;, y=&#039;Price&#039;, hue=&#039;Region&#039;, palette=&#039;bright&#039;)<\/p>\n<p>plt.title(&#039;House Price Distribution by Region with Seaborn&#039;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Seaborn\u4e2d\uff0c<code>hue<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u5206\u7c7b\u53d8\u91cf\uff0c<code>palette<\/code>\u53c2\u6570\u63a7\u5236\u4e0d\u540c\u7c7b\u522b\u7684\u989c\u8272\u3002Seaborn\u4f1a\u81ea\u52a8\u4e3a\u6570\u636e\u4e2d\u7684\u7c7b\u522b\u5206\u914d\u989c\u8272\uff0c\u5e76\u5728\u56fe\u8868\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04<\/h3>\n<\/p>\n<p><p>\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u60f3\u8981\u81ea\u5b9a\u4e49\u5206\u7c7b\u7740\u8272\u7684\u989c\u8272\u6620\u5c04\uff0c\u4ee5\u9002\u5e94\u7279\u5b9a\u7684\u89c6\u89c9\u8981\u6c42\u6216\u54c1\u724c\u8272\u5f69\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04<\/p>\n<p>custom_palette = sns.color_palette(&quot;husl&quot;, df[&#039;Region&#039;].nunique())<\/p>\n<p>sns.scatterplot(data=df, x=&#039;Area&#039;, y=&#039;Price&#039;, hue=&#039;Region&#039;, palette=custom_palette)<\/p>\n<p>plt.title(&#039;Custom Color Mapping&#039;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>sns.color_palette<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u989c\u8272\u4e3b\u9898\u7684\u8c03\u8272\u677f\uff0c\u5e76\u901a\u8fc7<code>palette<\/code>\u53c2\u6570\u5e94\u7528\u5230\u6563\u70b9\u56fe\u4e0a\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u6216\u4e2a\u4eba\u559c\u597d\uff0c\u5bf9\u6563\u70b9\u56fe\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u989c\u8272\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a\u901a\u8fc7matplotlib\u6216seaborn\u5e93\uff0cPython\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5de5\u5177\u6765\u8fdb\u884c\u5206\u7c7b\u7740\u8272\u7684\u6563\u70b9\u56fe\u7ed8\u5236\u3002\u65e0\u8bba\u662f\u5229\u7528\u5185\u7f6e\u7684\u989c\u8272\u6620\u5c04\u529f\u80fd\u8fd8\u662f\u81ea\u5b9a\u4e49\u989c\u8272\u65b9\u6848\uff0c\u60a8\u90fd\u80fd\u591f\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\u5206\u7c7b\u95f4\u7684\u5173\u7cfb\u548c\u5dee\u5f02\u3002\u8fd9\u4e0d\u4ec5\u589e\u5f3a\u4e86\u56fe\u8868\u7684\u4fe1\u606f\u8868\u8fbe\u80fd\u529b\uff0c\u4e5f\u4e3a\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u63d0\u4f9b\u4e86\u91cd\u8981\u7684\u89c6\u89c9\u8f85\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5206\u7c7b\u7684\u6563\u70b9\u56fe\u5e76\u8fdb\u884c\u7740\u8272\uff1f<\/strong><\/p>\n<p>\u5206\u7c7b\u7740\u8272\u7684\u6563\u70b9\u56fe\u53ef\u4ee5\u5f88\u76f4\u89c2\u5730\u5c55\u793a\u4e0d\u540c\u7c7b\u522b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff08\u5982Matplotlib\u6216Seaborn\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u636e\u6309\u7167\u7c7b\u522b\u8fdb\u884c\u5206\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684groupby\u51fd\u6570\u6216\u8005NumPy\u5e93\u7684where\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6839\u636e\u4e0d\u540c\u7684\u7c7b\u522b\uff0c\u9009\u62e9\u5408\u9002\u7684\u7740\u8272\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684scatter\u51fd\u6570\uff0c\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u6216\u8005\u4f7f\u7528Seaborn\u7684lmplot\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6839\u636e\u9700\u8981\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u3001\u56fe\u4f8b\u7b49\u6765\u589e\u52a0\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<p><strong>2. Python\u4e2d\u5982\u4f55\u7ed8\u5236\u591a\u7c7b\u522b\u6563\u70b9\u56fe\u5e76\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\uff1f<\/strong><\/p>\n<p>\u7ed8\u5236\u591a\u7c7b\u522b\u6563\u70b9\u56fe\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u5e93\u3002\u9996\u5148\uff0c\u5c06\u6570\u636e\u6309\u7167\u7c7b\u522b\u8fdb\u884c\u5206\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684groupby\u51fd\u6570\u6216NumPy\u7684where\u51fd\u6570\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684scatter\u51fd\u6570\u6216Seaborn\u7684lmplot\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5728scatter\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528c\u53c2\u6570\u6765\u6307\u5b9a\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u989c\u8272\u3002\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u540d\u79f0\u3001\u989c\u8272\u7f16\u7801\u6216\u989c\u8272\u6620\u5c04\u51fd\u6570\u6765\u5b9a\u4e49\u989c\u8272\u3002<\/p>\n<p>\u5728lmplot\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528hue\u53c2\u6570\u6765\u6307\u5b9a\u7c7b\u522b\u5217\uff0c\u5e76\u4e14\u5b83\u4f1a\u81ea\u52a8\u4e3a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u70b9\u9009\u62e9\u4e0d\u540c\u7684\u989c\u8272\u3002<\/p>\n<p>\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u548c\u56fe\u4f8b\u7b49\u6765\u63d0\u9ad8\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5206\u7c7b\u6563\u70b9\u56fe\u5e76\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u6307\u5b9a\u4e0d\u540c\u7684\u989c\u8272\u548c\u5f62\u72b6\uff1f<\/strong><\/p>\n<p>\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u6307\u5b9a\u4e0d\u540c\u7684\u989c\u8272\u548c\u5f62\u72b6\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u52a0\u5f3a\u5206\u7c7b\u6563\u70b9\u56fe\u7684\u53ef\u89c6\u5316\u6548\u679c\u3002\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6309\u7167\u7c7b\u522b\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528Pandas\u7684groupby\u51fd\u6570\u6216NumPy\u7684where\u51fd\u6570\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684scatter\u51fd\u6570\u6216Seaborn\u7684lmplot\u51fd\u6570\u6765\u7ed8\u5236\u6563\u70b9\u56fe\u3002\u5bf9\u4e8escatter\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528c\u53c2\u6570\u6765\u6307\u5b9a\u989c\u8272\uff0cs\u53c2\u6570\u6765\u6307\u5b9a\u5f62\u72b6\u5927\u5c0f\u3002\u5bf9\u4e8elmplot\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528hue\u53c2\u6570\u6765\u6307\u5b9a\u989c\u8272\uff0cmarkers\u53c2\u6570\u6765\u6307\u5b9a\u5f62\u72b6\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6839\u636e\u9700\u8981\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u3001\u56fe\u4f8b\u7b49\u6765\u63d0\u9ad8\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684title\u3001xlabel\u3001ylabel\u548clegend\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6563\u70b9\u56fe\u5206\u7c7b\u7740\u8272\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u6216seaborn\u7b49\u5e93\u6765\u5b9e\u73b0\uff0c\u5173\u952e\u5728\u4e8e\u58f0\u660e\u989c\u8272\u6620\u5c04\u3001\u5229 [&hellip;]","protected":false},"author":3,"featured_media":176240,"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\/176234"}],"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=176234"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176240"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}