{"id":6271,"date":"2017-08-11T16:02:55","date_gmt":"2017-08-11T08:02:55","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6271"},"modified":"2017-08-11T16:02:55","modified_gmt":"2017-08-11T08:02:55","slug":"python%e7%94%bb%e5%9b%be%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6271.html","title":{"rendered":"Python\u753b\u56fe\u7b80\u4ecb"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecdmatplotlib\u5e93\u4e2dpyplot\u6a21\u5757\u7684\u8fd0\u7528\uff0c\u4ee5\u53ca\u66f4\u4e3a\u7f8e\u89c2\u7684seaborn\u5e93\u3002\u548c\u4e00\u4e9b\u4f8b\u5b50\u7684\u5c55\u793a\u3002<br \/>\n\u6570\u636e\u53ef\u89c6\u5316\u662f\u5f53\u4e0b\u4e00\u4e2a\u63a2\u7d22\u3001\u5c55\u793a\u7ed3\u679c\u7684\u6709\u529b\u5de5\u5177\uff0c\u5bf9\u4e8e\u516c\u53f8\u800c\u8a00\uff0c\u505a\u597d\u6570\u636e\u53ef\u89c6\u5316\u80fd\u591f\u66f4\u597d\u5730\u5bf9\u5ba2\u6237\u4ecb\u7ecd\u81ea\u8eab\u670d\u52a1\uff0c\u4e5f\u80fd\u66f4\u597d\u5730\u5411\u987e\u5ba2\u5c55\u793a\u4fe1\u606f\u3002<\/p>\n<p>\u4e0b\u56fe\u662f\u4e00\u4e2a\u54a8\u8be2\u516c\u53f8\u505a\u7684\u4e00\u5f20\u7f8e\u56fdpizza\u5feb\u9910\u5e97\u7684\u5206\u5e03\u56fe\uff0c\u4ece\u56fe\u4e2d\u6211\u4eec\u80fd\u8f83\u4e3a\u76f4\u89c2\u7684\u770b\u51fa\u4e0d\u540c\u5feb\u9910\u5e97\u5728\u7f8e\u56fd\u4e0d\u540c\u5730\u533a\u7684\u5bc6\u5ea6\u548c\u5206\u5e03\u60c5\u51b5\uff0c\u66f4\u4e3a\u91cd\u8981\u7684\u662f\u2014\u2014\u5b83\u5438\u5f15\u773c\u7403\u3002<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471335196958.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6272\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471335196958.png\" alt=\"1471335196958\" width=\"954\" height=\"653\" \/><\/a><\/p>\n<h1>matplotlib<\/h1>\n<p>\u9996\u5148\u4ecb\u7ecd\u7684\u662fPython\u4e2d\u6700\u5e38\u7528\u4e5f\u6700\u57fa\u7840\u7684\u7528\u4e8e\u753b\u56fe\u7684\u5e93\u2014\u2014Matplotlib\uff0c\u8fd9\u662f\u4e00\u4e2apython\u7684\u56fe\u5f62\u6846\u67b6\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u4e00\u4e9b\u56fe\u5f62\u7684\u7ed8\u5236\uff0c\u6211\u4eec\u4e3b\u8981\u7528\u5230\u5176\u4e2d\u7684pyplot\u6a21\u5757\u3002<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u8bb8\u591a\u57fa\u7840\u56fe\u8868\u7684\u7ed8\u5236\uff0c\u53ef\u5b9a\u5236\u53c2\u6570\u8f83\u591a\uff08\u8be6\u89c1\u6587\u6863\uff09\uff0c\u4f46\u4f7f\u7528\u4e5f\u8f83\u590d\u6742\uff0c\u672c\u6587\u53ea\u9488\u5bf9\u56fe\u5f62\u7684\u5c55\u793a\uff0c\u5bf9pyplot\u6837\u5f0f\u7684\u5b9a\u5236\u6682\u4e0d\u6d89\u53ca\uff0c\u6216\u7531\u4e4b\u540e\u7684seaborn\u5e93\u66f4\u597d\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5b89\u88c5\uff1apip install matplotlib<br \/>\n\u4f7f\u7528\uff1a import matplotlib.pyplot as plt<\/p>\n<h2>\u6298\u7ebf\u56fe&amp;\u6563\u70b9\u56fe<\/h2>\n<p>\u6298\u7ebf\u56fe\uff1aplot()\u51fd\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nage = &#x5B;5, 10, 15, 20, 25, 30]\r\nheight = &#x5B;25, 45, 65, 75, 75, 75]\r\nplt.plot(age, height)  # \u4ee5age\u4e3a\u6a2a\u8f74\uff0cheight\u4e3a\u7eb5\u8f74\u753b\u51fa\u6298\u7ebf\u56fe \r\nplt.show()\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/line-chart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6289\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/line-chart.png\" alt=\"line chart\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u6563\u70b9\u56fe\uff1ascatter()\u51fd\u6570<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nweight = &#x5B;600,150,200,300,200,100,125,180]\r\nheight = &#x5B;60,65,73,70,65,58,66,67]  # \u4ee5weight\u4e3a\u6a2a\u8f74\uff0cheight\u4e3a\u7eb5\u8f74\u753b\u51fa\u6298\u7ebf\u56fe\r\nplt.scatter(height, weight)\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/scatterplot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6290\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/scatterplot.png\" alt=\"scatterplot\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<h2>\u6761\u5f62\u56fe<\/h2>\n<p>\u6761\u5f62\u56fe\uff1aplt.bar(x, y) , plt.hbar(x, y)<\/p>\n<p>\u7531\u4e8ematplotlib\u5e93\u7684\u7279\u6027\uff0cbar()\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ea\u80fd\u662f\u6570\u5b57\u6784\u6210\u7684\u5217\u8868\uff0c\u800c\u4e0d\u80fd\u662fstring\u3002\u4ece\u800c\u5728\u4e0b\u56fe\u7684\u7ed8\u5236\u4e2d\uff0c\u6211\u4eec\u5f15\u5165\u4e86x\u53d8\u91cf\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nnames = &#x5B;&quot;McDonalds&quot;, &quot;Burger King&quot;, &quot;Wendys&quot;, &quot;Subway&quot;]\r\npatrons = &#x5B;10000, 5000, 5000, 7500]\r\nx = &#x5B;0, 1, 2, 3]\r\nplt.bar(x, patrons)\r\nplt.show()\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/bar-chart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6280\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/bar-chart.png\" alt=\"bar chart\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nplt.hbar(x, patrons)  # horizontal\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/barh.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6281\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/barh.png\" alt=\"barh\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<h2>\u56fe\u6ce8<\/h2>\n<p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u56fe\u6807\u52a0\u4e00\u4e9b\u6807\u7b7e\uff1a<br \/>\n&#8211; \u6807\u9898\uff1atitle()\u65b9\u6cd5<br \/>\n&#8211; x, y\u8f74\u6807\u7b7e\uff1a xlabel(), ylabel()\u65b9\u6cd5<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nweight = &#x5B;600,150,200,300,200,100,125,180]\r\nheight = &#x5B;60,65,73,70,65,58,66,67]  # \u4ee5weight\u4e3a\u6a2a\u8f74\uff0cheight\u4e3a\u7eb5\u8f74\u753b\u51fa\u6298\u7ebf\u56fe\r\nplt.scatter(height, weight)\r\nplt.xlabel('weight')\r\nplt.ylabel('height')\r\nplt.title(&quot;height vs weight&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_labels.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6287\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_labels.png\" alt=\"figure_labels\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<h2>\u56fe\u8868\u6837\u5f0f<\/h2>\n<p>\u901a\u8fc7style.use() \u51fd\u6570\uff0c\u6211\u4eec\u80fd\u591f\u5bf9\u56fe\u8868\u7684\u6837\u5f0f\u8fdb\u884c\u5b9a\u5236\u3002Matplotlib\u5e93\u6709\u4e00\u4e9b\u5185\u7f6e\u7684\u6837\u5f0f\u4f9b\u6211\u4eec\u4f7f\u7528\uff1a<\/p>\n<ul>\n<li>ggplot: R\u8bed\u8a00\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u7684\u6837\u5f0f<\/li>\n<li>bmh: \u7edf\u8ba1\u5b66\u4e66\u7c4d\u4e2d\u5e38\u7528\u7684\u6837\u554a\u662f<\/li>\n<li>dark_background: \u4e3a\u56fe\u8868\u63d0\u4f9b\u4e00\u4e2a\u66f4\u6697\u7684\u80cc\u666f<\/li>\n<\/ul>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nweight = &#x5B;600, 150, 200, 300, 200, 100, 125, 180]\r\nheight = &#x5B;60, 65, 73, 70, 65, 58, 66, 67]  \r\nplt.scatter(height, weight)\r\nplt.style.use(&quot;ggplot&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/ggplot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6288\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/ggplot.png\" alt=\"ggplot\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<h2>\u5b9e\u4f8b<\/h2>\n<p>\u672c\u4f8b\u5b50\u5c06\u5728\u62e5\u6709\u4e00\u4e2a\u6570\u636e\u96c6\uff08recent-grads.csv\uff09\u7684\u57fa\u7840\u4e0a\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u5bfc\u5165\u3001\u57fa\u7840\u5236\u56fe\u5206\u6790\u7684\u64cd\u4f5c\u3002<br \/>\n\u672c\u4f8b\u7528\u5230\u7684\u6570\u636e\u53ef\u5728<a href=\"https:\/\/github.com\/fivethirtyeight\/data\/tree\/master\/college-majors\">\u8fd9\u91cc\u4e0b\u8f7d<\/a>\u3002<\/p>\n<h3>\u5bfc\u5165\u6570\u636e<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport pandas as pd\r\nrecent_grads = pd.read_csv('recent-grads.csv')\r\nprint recent_grads.head(3) # \u8f93\u51fa\u5bfc\u5165\u6570\u636e\u7684\u524d\u4e09\u884c\r\n<\/pre>\n<p><a href=\"http:\/\/pandas.pydata.org\/pandas-docs\/stable\/10min.html\">Pandas<\/a>\u662f\u6570\u636e\u5206\u6790\u4e2d\u5341\u5206\u5e38\u7528\u7684\u4e00\u4e2a\u5e93\uff0c\u8fd9\u91cc\u4ec5\u4ec5\u7528\u5230\u5b83\u7684\u5bfc\u5165\u6570\u636e\u529f\u80fd\uff0c\u6570\u636e\u88ab\u5bfc\u5165\u540e\u5c06\u5b58\u4e3aDataFrame\u683c\u5f0f\uff0c\u8fd9\u662fpandas\u4e2d\u4e00\u79cd\u7c7b\u8868\u683c\u3001\u975e\u5e38\u6613\u4e8e\u64cd\u4f5c\u7684\u6570\u636e\u683c\u5f0f\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff08\u4ee5\u4e0a\u4ee3\u7801\u7684\u8f93\u51fa\uff09\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471403239623.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6273\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471403239623.png\" alt=\"1471403239623\" width=\"816\" height=\"577\" \/><\/a><\/p>\n<p>\u6bcf\u4e00\u5217\u4ee3\u8868\u4e00\u4e2a\u4e13\u4e1a\uff0c\u4ee5\u53ca\u8fd9\u4e2a\u4e13\u4e1a\u7684\u4eba\u6570\u3001\u5c31\u4e1a\u7387\u3001\u5de5\u8d44\u6c34\u5e73\u7b49\u4fe1\u606f<\/p>\n<h3>\u7ed8\u5236\u76f4\u65b9\u56fe<\/h3>\n<p>\u76f4\u65b9\u56fe\uff1aDataFrame.hist()<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nplt.style.use('ggplot')\r\ncolumns = &#x5B;'Median','Sample_size']\r\nrecent_grads.hist(column=columns) #pandas\u4e2d\u7ed8\u5236\u76f4\u65b9\u56fe\u7684\u65b9\u6cd5\uff0c\u53c2\u6570\u8868\u793a\u6570\u636e\u6e90\uff0c\u5373\u9488\u5bf9\u54ea\u4e00\u5217\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6284\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_1.png\" alt=\"figure_1\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u4ee5\u4e0a\u662f\u5404\u4e13\u4e1a\u5de5\u8d44\u4e2d\u4f4d\u6570\u548c\u8c03\u67e5\u6837\u672c\u76f4\u65b9\u56fe\uff0c\u4f46\u6211\u4eec\u53d1\u73b0\u6a2a\u5750\u6807\u7684\u6570\u5b57\u7531\u4e8e\u8fc7\u4e8e\u5bc6\u96c6\u5bfc\u81f4\u65e0\u6cd5\u770b\u6e05\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u56fe\u7247\u5e03\u5c40\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nrecent_grads.hist(column=columns, layout=(2,1), grid=False)\r\n<\/pre>\n<p>layout\u53c2\u6570\u8868\u793a\u56fe\u7247\u662f\u4ee52&#215;1\u7684\u65b9\u5f0f\u6392\u5217\uff0cgrid\u53c2\u6570\u5219\u8bbe\u5b9a\u56fe\u8868\u4e2d\u662f\u5426\u6709\u7f51\u683c\u3002<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6285\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_2.png\" alt=\"figure_2\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u6b64\u65f6\u5750\u6807\u8f74\u7684\u95ee\u9898\u89e3\u51b3\u4e86\uff0c\u4f46\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u56fe\u8868\u592a\u7c97\u7cd9\u4e86\uff0c\u76f4\u65b9\u56fe\u4e2d\u6bcf\u4e00\u5217\u8986\u76d6\u7684\u8303\u56f4\u592a\u5927\u4e86\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5f97\u5230\u66f4\u7ec6\u81f4\u7684\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528bin\u53c2\u6570\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nrecent_grads.hist(column=&quot;Sample_size&quot;, bins=50)  # \u5c06\u76f4\u65b9\u56fe\u5206\u621050\u680f\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6286\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/figure_3.png\" alt=\"figure_3\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u5728\u8fd9\u4e2a\u56fe\u4e2d\u6211\u4eec\u770b\u5230\u4e86\u4e4b\u524d\u770b\u4e0d\u5230\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u901a\u8fc7\u76f4\u65b9\u56fe\u6211\u4eec\u80fd\u770b\u51fa\u6570\u636e\u7684\u5927\u6982\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<h3>\u7bb1\u5f62\u56fe<\/h3>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5e0c\u671b\u7814\u7a76\u6bcf\u4e2a\u5927\u7c7b\uff08Major_category\uff09\u4e2d\u6837\u672c\u7684\u4ee3\u8868\u6027\u5f3a\u5f31\uff0c\u5373\u901a\u8fc7\u6bd4\u8f83sample_size\u548cTotal\u7684\u7bb1\u5f62\u56fe\uff0c\u6765\u770b\u6837\u672c\u4e2d\u6570\u636e\u7684\u5206\u5e03\u548c\u603b\u4f53\u7684\u5206\u5e03\u662f\u5426\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsample_size = recent_grads&#x5B;&#x5B;'Sample_size', 'Major_category']]  # \u9009\u62e9DataFrame\u4e2d\u7684\u5176\u4e2d\u4e24\u5217\uff0c\u9009\u62e9\u7684\u65b9\u5f0f\u548clist\u7c7b\u4f3c\uff0c\u53ea\u662findex\u53d8\u6210\u5217\u540d\r\nsample_size.boxplot(by='Major_category')\r\nplt.xticks(rotation=90)\r\nplt.show()\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/box_plot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6282\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/box_plot.png\" alt=\"box_plot\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u5728\u4ee5\u4e0a\u7684\u7a0b\u5e8f\u4e2d\uff0cPandas\u505a\u4e86\u4ee5\u4e0b\u8fd9\u4e9b\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u4ece\u5b8c\u6574\u7684\u6570\u636e\u4e2d\u7b5b\u9009\u51faSample_size\u548cMajor_category\u8fd9\u4e24\u680f<\/li>\n<li>\u5c06Sample_size\u4e2d\u7684\u6570\u636e\u6309\u5927\u7c7b\u5206\u7ec4\uff0c\u6bcf\u4e00\u4e2aSample_size\u5206\u5230\u552f\u4e00\u7684Major_category\uff0c\u5bf9\u5e94\u4ee3\u7801\u4e2d\u7684by=&#8217;Major_category&#8217;<\/li>\n<li>\u5bf9\u6bcf\u4e00\u4e2aMajor_category\uff0c\u7528\u8be5\u5927\u7c7b\u4e2d\u7684Sample_size\u6570\u636e\u751f\u6210\u7bb1\u5f62\u56fe<\/li>\n<li>\u5c06x\u8f74\u5750\u6807\u65cb\u8f6c90\u5ea6\uff0c\u907f\u514d\u56e0\u4e3a\u95f4\u8ddd\u8fc7\u7a84\u800c\u9020\u6210\u6587\u5b57\u91cd\u53e0<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nrecent_grads&#x5B;&#x5B;'Total', 'Major_category']].boxplot(by='Major_category')\r\nplt.xticks(rotation=90)\r\nplt.show()\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/box_plot2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6283\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/box_plot2.png\" alt=\"box_plot2\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u901a\u8fc7\u6bd4\u5bf9Sample_size\u548cTotal\u7684\u4e24\u5f20\u7bb1\u5f62\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6837\u672c\u7684\u5206\u5e03\u80fd\u591f\u57fa\u672c\u53cd\u6620\u51fa\u603b\u4f53\u7684\u5206\u5e03\uff08\u5373\u7bb1\u5f62\u56fe\u8f83\u4e3a\u7c7b\u4f3c\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u8ba4\u4e3a\u6837\u672c\u7684\u9009\u53d6\u662f\u5177\u6709\u4ee3\u8868\u6027\u7684\u3002<\/p>\n<h3>\u5c06\u591a\u5f20\u56fe\u753b\u5728\u4e00\u8d77<\/h3>\n<p>\u8fd9\u4e00\u6b65\u6211\u4eec\u5e0c\u671b\u63a2\u8ba8\u54ea\u4e9b\u56e0\u7d20\u80fd\u591f\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u5bf9\u6bd5\u4e1a\u751f\u7684\u5de5\u8d44Median\u6709\u4e00\u5b9a\u7684\u5f71\u54cd\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u6563\u70b9\u56fe\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nplt.scatter(recent_grads&#x5B;'Unemployment_rate'], recent_grads&#x5B;'Median'], color='red')  # \u4ee5Unemployment_rate\u4e3ax\u8f74\uff0c\u4ee5Median\u4e3ay\u8f74\uff0c\u753b\u7ea2\u8272\u7684\u6563\u70b9\u56fe\r\nplt.scatter(recent_grads&#x5B;'ShareWomen'], recent_grads&#x5B;'Median'], color='blue')\r\nplt.show()\r\n<\/pre>\n<p>\u753b\u7684\u4e24\u5f20\u56fe\u6709\u540c\u6837\u7684Y\u8f74\uff0c\u4e0d\u540c\u7684X\u8f74\uff0c\u6211\u4eec\u5728\u6700\u540e\u5e94\u7528plt.show()\u51fd\u6570\uff0c\u80fd\u591f\u628a\u4ed6\u4eec\u663e\u793a\u5728\u4e00\u5f20\u56fe\u4e0a\uff0c\u66f4\u597d\u5730\u8fdb\u884c\u5bf9\u6bd4\u3002<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/scatterplot2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6291\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/scatterplot2.png\" alt=\"scatterplot2\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>\u901a\u8fc7\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u5931\u4e1a\u7387\u548c\u5de5\u8d44\u95f4\u51e0\u4e4e\u6ca1\u6709\u76f8\u5173\u6027\uff0c\u800c\u5973\u6027\u5458\u5de5\u7387\u548c\u5de5\u8d44\u4e2d\u4f4d\u6570\u5219\u5b58\u5728\u5fae\u5f31\u7684\u7ebf\u6027\u76f8\u5173\u3002<\/p>\n<h1>Seaborn<\/h1>\n<p><a href=\"https:\/\/stanford.edu\/~mwaskom\/software\/seaborn\/index.html\">Seaborn<\/a>\u662f\u4e00\u4e2a\u7531\u65af\u5766\u798f\u5927\u5b66\u7814\u53d1\u7684\uff0c\u7528\u4e8e\u5236\u4f5cpresentation\u7ea7\u522b\u7f8e\u89c2\u5ea6\u7684\u6570\u636e\u53ef\u89c6\u5316\u7684\u5305\u3002Seaborn\u901a\u8fc7matplotlib\u5e93\u7684\u51fd\u6570\u6765\u64cd\u4f5c\u6570\u636e\u3001\u5b9a\u5236\u56fe\u8868\uff0c\u5236\u4f5c\u540c\u6837\u6837\u5f0f\u7684\u56fe\u8868\uff0cSeaborn\u6240\u9700\u7684\u4ee3\u7801\u8981\u5927\u5927\u51cf\u5c11\u3001\u4e5f\u7b80\u5355\u8bb8\u591a\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u56fe\u4f8b\u7684\u5c55\u793a\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471422969963.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6274\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471422969963.png\" alt=\"1471422969963\" width=\"615\" height=\"542\" \/><\/a><\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471423068972.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6275\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471423068972.png\" alt=\"1471423068972\" width=\"524\" height=\"376\" \/><\/a><\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471423208125.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6276\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471423208125.png\" alt=\"1471423208125\" width=\"829\" height=\"738\" \/><\/a><\/p>\n<p>\u66f4\u591a\u4f8b\u5b50\u8be6\u89c1<a href=\"http:\/\/stanford.edu\/~mwaskom\/software\/seaborn\/examples\/index.html\">Gallery<\/a>\u3002<\/p>\n<h2>\u4f9d\u8d56\u7684\u5e93<\/h2>\n<p>\u8981\u987a\u5229\u4f7f\u7528Seaborn\u5e93\uff0c\u987b\u5148\u5b89\u88c5numpy\u3001pandas\u3001matplotlib\u548cscipy\uff08\u5b89\u88c5Seaborn\u65f6\u4f1a\u81ea\u52a8\u5b89\u88c5\uff09\u5e93\u3002<\/p>\n<h2>Seaborn\u5b9e\u4f8b<\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u7528Seaborn\u7ed8\u5236\u4e00\u4e9b\u4e4b\u524d\u5c55\u793a\u8fc7\u7684\u56fe\u8868\uff0c\u501f\u6b64\u770b\u770b\u5176\u4e2d\u7684\u5dee\u522b\uff0c\u8bf4\u660e\u5c06\u653e\u5728\u4ee3\u7801\u7684\u6ce8\u91ca\u4e2d\u3002<\/p>\n<h3>\u76f4\u65b9\u56fe<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport seaborn as sns\r\nrecent_grads = pd.read_csv('recent-grads.csv')\r\nsns.distplot(recent_grads&#x5B;'Median'], kde=True)  # \u8ddf\u4e4b\u524d\u4ecb\u7ecd\u7684hist()\u51fd\u6570\u529f\u80fd\u76f8\u540c\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471486874661.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6277\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471486874661.png\" alt=\"1471486874661\" width=\"521\" height=\"356\" \/><\/a><\/p>\n<p>\u53c2\u6570kde=True\u5e2e\u6211\u4eec\u753b\u4e86\u4e00\u6761\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u7684\u4f30\u8ba1\u66f2\u7ebf\uff0crug=True\u4f7f\u5f97\u6bcf\u4e00\u4e2a\u6570\u636e\u4f1a\u5728\u56fe\u8868\u5e95\u90e8\u5bf9\u5e94\u7684X\u503c\u5904\u753b\u4e0a\u4e00\u5212\u3002\u9664\u6b64\u4e4b\u5916\uff0cSeaborn\u80fd\u6839\u636e\u6570\u636e\u7684\u60c5\u51b5\u81ea\u52a8\u786e\u5b9abin\u503c\uff0c\u514d\u53bb\u4e4b\u524d\u8c03\u6574\u7684\u70e6\u607c\u3002<\/p>\n<h3>\u7bb1\u5f62\u56fe<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ns = sns.boxplot(recent_grads&#x5B;&quot;Major_category&quot;], recent_grads&#x5B;&quot;Total&quot;])\r\nfor item in s.get_xticklabels():  # \u83b7\u53d6X\u8f74\u4e0a\u7684\u6807\u7b7e\r\n    item.set_rotation(45)  #\u5c06\u6807\u7b7e\u65cb\u8f6c45\u5ea6\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471487476317.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6278\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471487476317.png\" alt=\"1471487476317\" width=\"530\" height=\"468\" \/><\/a><\/p>\n<h3>Pairplot<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndata = recent_grads&#x5B;&#x5B;'Median', 'ShareWomen', 'P25th']]\r\nsns.pairplot(data)\r\n<\/pre>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471488086732.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6279\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2017\/08\/1471488086732.png\" alt=\"1471488086732\" width=\"545\" height=\"529\" \/><\/a><\/p>\n<p>\u4e0a\u56fe\u7684data\u4e2d\u6211\u4eec\u7b5b\u9009\u4e86\u4e09\u680f\uff0c\u4e5f\u5c31\u662f\u4e09\u4e2a\u53d8\u91cf\u2014\u2014\u5de5\u8d44\u4e2d\u4f4d\u6570\u3001\u5973\u6027\u5360\u6bd4\u548c<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E5%9B%9B%E5%88%86%E4%BD%8D%E6%95%B0\">\u8f83\u5c0f\u56db\u5206\u6570\u4f4d<\/a>\uff0c\u5176\u4e2d\u8f83\u5c0f\u56db\u5206\u4f4d\u6570\u53cd\u6620\u7684\u662f\u8be5\u4e13\u4e1a\u5de5\u8d44\u4f4e\u6536\u5165\u90e8\u5206\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5728\u7ed8\u5236\u7684pairplot\u4e2d\uff0c\u5982\u679c\u662f\u4e00\u4e2a\u53d8\u91cf\u5bf9\u53e6\u4e00\u4e2a\u53d8\u91cf\uff08\u5982Median vs ShareWomen\uff09\u90e8\u5206\uff0c\u5219\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5982\u679c\u662f\u540c\u4e00\u53d8\u91cf\u5219\u7ed8\u5236\u76f4\u65b9\u56fe\u3002<\/p>\n<p>\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51faMedian\u548cP25th\u6210\u8f83\u5f3a\u7684\u7ebf\u6027\u76f8\u5173\uff0c\u5973\u6027\u5360\u6bd4\u548c\u5de5\u8d44\u7684\u4e2d\u95f4\u6c34\u5e73\u3001\u4f4e\u6536\u5165\u90e8\u5206\u90fd\u5448\u8d1f\u7ebf\u6027\u76f8\u5173\u3002<\/p>\n<h1>\u5efa\u8bae<\/h1>\n<p>\u5982\u679c\u5e0c\u671b\u5f00\u59cb\u505a\u6570\u636e\u5206\u6790\u7684\u8bdd\uff0c\u5efa\u8bae\u76f4\u63a5\u4eceSeaborn\u5e93\u5f00\u59cb\u5b66\u4e60\uff0cmatplotlib\u505a\u4e86\u89e3\u5373\u53ef\u3002\u53ea\u6709\u5f53\u505a\u4f9b\u81ea\u5df1\u5206\u6790\u6216Seaborn\u65e0\u6cd5\u5b8c\u6210\u7684\u56fe\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u7528matplotlib+Pandas\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecdmatplotlib\u5e93\u4e2dpyplot\u6a21\u5757\u7684\u8fd0\u7528\uff0c\u4ee5\u53ca\u66f4\u4e3a\u7f8e\u89c2\u7684seaborn\u5e93\u3002\u548c\u4e00\u4e9b\u4f8b\u5b50\u7684\u5c55\u793a\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6271","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6271"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6271\/revisions"}],"predecessor-version":[{"id":6292,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6271\/revisions\/6292"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}