{"id":1002333,"date":"2024-12-27T10:07:46","date_gmt":"2024-12-27T02:07:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002333.html"},"modified":"2024-12-27T10:07:49","modified_gmt":"2024-12-27T02:07:49","slug":"python-%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e8%af%8d%e4%ba%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1002333.html","title":{"rendered":"python \u5982\u4f55\u5236\u4f5c\u8bcd\u4e91"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080133\/850a2f36-36c3-4b4d-a09f-60c4391b4ea4.webp\" alt=\"python \u5982\u4f55\u5236\u4f5c\u8bcd\u4e91\" \/><\/p>\n<p><p> <strong>\u5236\u4f5c\u8bcd\u4e91\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u4e2a\u5e93\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fWordCloud\u5e93\u3002\u901a\u8fc7WordCloud\u5e93\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u6587\u672c\u6570\u636e\u4e2d\u751f\u6210\u8bcd\u4e91\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u76f8\u5173\u7684\u5e93\uff0c\u7136\u540e\u5bfc\u5165\u6240\u9700\u7684\u5e93\uff0c\u51c6\u5907\u597d\u6587\u672c\u6570\u636e\uff0c\u63a5\u7740\u751f\u6210\u5e76\u53ef\u89c6\u5316\u8bcd\u4e91\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5236\u4f5c\u8bcd\u4e91\u7684\u5177\u4f53\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u5e93<\/p>\n<\/p>\n<p><p>\u8981\u5236\u4f5c\u8bcd\u4e91\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9bPython\u5e93\u3002\u4e3b\u8981\u5e93\u5305\u62ecWordCloud\u3001Matplotlib\u548cPillow\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wordcloud matplotlib pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wordcloud import WordCloud<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907\u6587\u672c\u6570\u636e<\/p>\n<\/p>\n<p><p>\u8bcd\u4e91\u7684\u751f\u6210\u9700\u8981\u6587\u672c\u6570\u636e\u3002\u6587\u672c\u6570\u636e\u53ef\u4ee5\u662f\u4efb\u610f\u5f62\u5f0f\u7684\u6587\u5b57\u5185\u5bb9\uff0c\u5982\u4e00\u4e2a\u5b57\u7b26\u4e32\u3001\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u6216\u4ece\u7f51\u9875\u4e0a\u6293\u53d6\u7684\u5185\u5bb9\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7b80\u5355\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a great programming language. It is widely used for data analysis, machine learning, web development, and more.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u751f\u6210\u8bcd\u4e91<\/p>\n<\/p>\n<p><p><strong>\u751f\u6210\u8bcd\u4e91\u7684\u6838\u5fc3\u6b65\u9aa4\u662f\u521b\u5efaWordCloud\u5bf9\u8c61\uff0c\u5e76\u5c06\u6587\u672c\u6570\u636e\u4f20\u9012\u7ed9\u5b83\u3002<\/strong>\u53ef\u4ee5\u4f7f\u7528WordCloud\u5e93\u4e2d\u7684WordCloud\u7c7b\uff0c\u5e76\u914d\u7f6e\u4e00\u4e9b\u53c2\u6570\uff0c\u5982\u8bcd\u4e91\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u80cc\u666f\u989c\u8272\u3001\u6700\u5927\u8bcd\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, max_words=200).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u53ef\u89c6\u5316\u8bcd\u4e91<\/p>\n<\/p>\n<p><p>\u751f\u6210\u8bcd\u4e91\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u901a\u5e38\u4f7f\u7528Matplotlib\u5e93\u6765\u663e\u793a\u8bcd\u4e91\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Matplotlib\u663e\u793a\u751f\u6210\u7684\u8bcd\u4e91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(10, 5))<\/p>\n<p>plt.imshow(wordcloud, interpolation=&#39;bilinear&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u81ea\u5b9a\u4e49\u8bcd\u4e91\u5f62\u72b6<\/p>\n<\/p>\n<p><p><strong>\u9664\u4e86\u57fa\u672c\u7684\u77e9\u5f62\u8bcd\u4e91\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\u7684\u8499\u7248\u56fe\u50cf\u6765\u521b\u5efa\u5177\u6709\u7279\u5b9a\u5f62\u72b6\u7684\u8bcd\u4e91\u3002<\/strong>\u4ee5\u4e0b\u662f\u5982\u4f55\u5236\u4f5c\u5177\u6709\u81ea\u5b9a\u4e49\u5f62\u72b6\u7684\u8bcd\u4e91\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u51c6\u5907\u4e00\u5f20\u9ed1\u767d\u56fe\u50cf\u4f5c\u4e3a\u8499\u7248\uff0c\u5176\u4e2d\u767d\u8272\u90e8\u5206\u5b9a\u4e49\u8bcd\u4e91\u7684\u5f62\u72b6\u3002<\/li>\n<li>\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">mask = np.array(Image.open(&#39;path_to_mask_image.png&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u751f\u6210\u5177\u6709\u81ea\u5b9a\u4e49\u5f62\u72b6\u7684\u8bcd\u4e91\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, max_words=200, mask=mask, contour_color=&#39;black&#39;, contour_width=1).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528WordCloud\u5e93\u521b\u5efa\u57fa\u672c\u548c\u81ea\u5b9a\u4e49\u5f62\u72b6\u7684\u8bcd\u4e91\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u8bcd\u4e91\u7684\u9ad8\u7ea7\u914d\u7f6e<\/p>\n<\/p>\n<p><p><strong>\u5728\u751f\u6210\u8bcd\u4e91\u65f6\uff0c\u53ef\u4ee5\u914d\u7f6e\u8bb8\u591a\u5176\u4ed6\u53c2\u6570\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63a7\u5236\u8bcd\u4e91\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002<\/strong>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u9ad8\u7ea7\u914d\u7f6e\u9009\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u4f53\u8def\u5f84<\/strong>\uff1a\u6307\u5b9a\u751f\u6210\u8bcd\u4e91\u65f6\u4f7f\u7528\u7684\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(font_path=&#39;path_to_font.ttf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u989c\u8272\u6620\u5c04<\/strong>\uff1a\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\u6620\u5c04\u6765\u751f\u6210\u8bcd\u4e91\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(colormap=&#39;plasma&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u505c\u7528\u8bcd\uff08Stopwords\uff09<\/strong>\uff1a\u6307\u5b9a\u4e0d\u5728\u8bcd\u4e91\u4e2d\u663e\u793a\u7684\u8bcd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wordcloud import STOPWORDS<\/p>\n<p>stopwords = set(STOPWORDS)<\/p>\n<p>stopwords.update([&#39;Python&#39;, &#39;language&#39;])<\/p>\n<p>wordcloud = WordCloud(stopwords=stopwords)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u6587\u672c\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u672c\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff08\u5982.txt\u6587\u4ef6\uff09\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6587\u672c\u6570\u636e\u5e76\u751f\u6210\u8bcd\u4e91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;path_to_text_file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, max_words=200).generate(text)<\/p>\n<p>plt.figure(figsize=(10, 5))<\/p>\n<p>plt.imshow(wordcloud, interpolation=&#39;bilinear&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u8bcd\u4e91\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6587\u672c\u5206\u6790\u3001\u5e02\u573a\u7814\u7a76\u548c\u793e\u4ea4\u5a92\u4f53\u5206\u6790\u7b49\u9886\u57df\u3002\u901a\u8fc7\u8bcd\u4e91\uff0c\u53ef\u4ee5\u5feb\u901f\u8bc6\u522b\u6587\u672c\u6570\u636e\u4e2d\u6700\u91cd\u8981\u6216\u6700\u5e38\u89c1\u7684\u8bcd\u6c47\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5236\u4f5c\u8bcd\u4e91\u65f6\uff0c\u9664\u4e86\u4f7f\u7528WordCloud\u5e93\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u6587\u672c\u5904\u7406\u5e93\uff08\u5982NLTK\u3001spaCy\uff09\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u8bcd\u4e91\u7684\u8d28\u91cf\u548c\u8868\u73b0\u529b\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u81ea\u5b9a\u4e49\u5f62\u72b6\uff0cPython\u4e2d\u7684\u8bcd\u4e91\u5236\u4f5c\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u6570\u636e\u6765\u5236\u4f5c\u8bcd\u4e91\uff1f<\/strong><br \/>\u5236\u4f5c\u8bcd\u4e91\u7684\u7b2c\u4e00\u6b65\u662f\u9009\u62e9\u9002\u5408\u7684\u6587\u672c\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u535a\u5ba2\u6587\u7ae0\u3001\u793e\u4ea4\u5a92\u4f53\u8bc4\u8bba\u3001\u4e66\u7c4d\u6458\u5f55\u6216\u4efb\u4f55\u5176\u4ed6\u5f62\u5f0f\u7684\u6587\u672c\u3002\u786e\u4fdd\u6240\u9009\u6587\u672c\u5185\u5bb9\u8db3\u591f\u4e30\u5bcc\uff0c\u8fd9\u6837\u53ef\u4ee5\u751f\u6210\u66f4\u5177\u4ee3\u8868\u6027\u7684\u8bcd\u4e91\u3002\u6b64\u5916\uff0c\u6587\u672c\u7684\u4e3b\u9898\u5e94\u5f53\u4e0e\u4f60\u7684\u76ee\u6807\u53d7\u4f17\u76f8\u5173\uff0c\u8fd9\u6837\u751f\u6210\u7684\u8bcd\u4e91\u624d\u80fd\u66f4\u6709\u6548\u5730\u4f20\u8fbe\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5236\u4f5c\u8bcd\u4e91\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u5236\u4f5c\u8bcd\u4e91\u901a\u5e38\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u7279\u5b9a\u7684Python\u5e93\u3002\u6700\u5e38\u7528\u7684\u5e93\u662f<code>wordcloud<\/code>\uff0c\u5b83\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u8bcd\u4e91\u56fe\u50cf\u3002\u5176\u4ed6\u5e38\u89c1\u7684\u5e93\u5305\u62ec<code>matplotlib<\/code>\u7528\u4e8e\u663e\u793a\u56fe\u50cf\uff0c<code>pandas<\/code>\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u4ee5\u53ca<code>nltk<\/code>\u6216<code>re<\/code>\u7528\u4e8e\u6587\u672c\u9884\u5904\u7406\u3002\u786e\u4fdd\u5728\u5f00\u59cb\u4e4b\u524d\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u4ee5\u4fbf\u987a\u5229\u8fdb\u884c\u8bcd\u4e91\u5236\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u8bcd\u4e91\u8fdb\u884c\u7f8e\u5316\u548c\u5b9a\u5236\uff1f<\/strong><br \/>\u8bcd\u4e91\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u7f8e\u5316\u548c\u5b9a\u5236\u3002\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u5b57\u4f53\u3001\u989c\u8272\u548c\u5f62\u72b6\u6765\u589e\u5f3a\u89c6\u89c9\u6548\u679c\u3002\u4f7f\u7528<code>wordcloud<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u8c03\u6574\u989c\u8272\u65b9\u6848\u3001\u80cc\u666f\u8272\u548c\u5f62\u72b6\u56fe\u6848\u3002\u6b64\u5916\uff0c\u5229\u7528<code>matplotlib<\/code>\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u56fe\u50cf\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u8bcd\u4e91\u66f4\u5177\u5438\u5f15\u529b\u3002\u8003\u8651\u4f7f\u7528\u5728\u7ebf\u8bcd\u4e91\u751f\u6210\u5668\u4f5c\u4e3a\u53c2\u8003\uff0c\u83b7\u53d6\u7075\u611f\u548c\u8bbe\u8ba1\u601d\u8def\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5236\u4f5c\u8bcd\u4e91\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u4e2a\u5e93\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fWordCloud\u5e93\u3002\u901a\u8fc7WordCloud\u5e93\uff0c\u53ef\u4ee5\u975e\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1002339,"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\/1002333"}],"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=1002333"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002333\/revisions"}],"predecessor-version":[{"id":1002344,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002333\/revisions\/1002344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002339"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}