{"id":1061065,"date":"2024-12-31T15:39:38","date_gmt":"2024-12-31T07:39:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061065.html"},"modified":"2024-12-31T15:39:42","modified_gmt":"2024-12-31T07:39:42","slug":"%e6%9c%80%e7%ae%80%e5%8d%95%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e8%af%8d%e4%ba%91%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061065.html","title":{"rendered":"\u6700\u7b80\u5355\u5982\u4f55\u7528python\u505a\u8bcd\u4e91\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/513f20be-a914-4167-ac77-15ee244aa0c6.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u6700\u7b80\u5355\u5982\u4f55\u7528python\u505a\u8bcd\u4e91\u56fe\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5236\u4f5c\u8bcd\u4e91\u56fe\u6709\u5f88\u591a\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528WordCloud\u5e93\u3002<strong>\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u5bfc\u5165\u6570\u636e\u3001\u751f\u6210\u8bcd\u4e91\u56fe\u3001\u4fdd\u5b58\u8bcd\u4e91\u56fe<\/strong>\u662f\u6700\u6838\u5fc3\u7684\u6b65\u9aa4\u3002\u5728\u8fd9\u56db\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u6700\u9700\u8981\u8be6\u7ec6\u63cf\u8ff0\u7684\u662f\u751f\u6210\u8bcd\u4e91\u56fe\u7684\u8fc7\u7a0b\uff0c\u56e0\u4e3a\u8fd9\u4e00\u6b65\u6d89\u53ca\u5230\u6570\u636e\u9884\u5904\u7406\u548c\u56fe\u5f62\u53c2\u6570\u7684\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u3002\u4e3b\u8981\u7684\u5e93\u5305\u62ecWordCloud\u3001matplotlib\u548cPillow\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install wordcloud matplotlib Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u6216\u5176\u4ed6\u683c\u5f0f\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4f5c\u4e3a\u6570\u636e\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = open(&#39;sample.txt&#39;, &#39;r&#39;).read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u751f\u6210\u8bcd\u4e91\u56fe<\/p>\n<\/p>\n<p><p>\u751f\u6210\u8bcd\u4e91\u56fe\u662f\u6574\u4e2a\u8fc7\u7a0b\u7684\u6838\u5fc3\u90e8\u5206\u3002\u8fd9\u4e00\u6b65\u6d89\u53ca\u5230\u6570\u636e\u9884\u5904\u7406\u3001\u8bbe\u7f6e\u8bcd\u4e91\u56fe\u7684\u5f62\u72b6\u3001\u989c\u8272\u548c\u5176\u4ed6\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;).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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u8bcd\u4e91\u56fe\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u4e3a800&#215;400\u50cf\u7d20\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272\u3002\u751f\u6210\u7684\u8bcd\u4e91\u56fe\u5c06\u663e\u793a\u5728\u4e00\u4e2a10&#215;5\u82f1\u5bf8\u7684\u7a97\u53e3\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4fdd\u5b58\u8bcd\u4e91\u56fe<\/p>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u5c06\u751f\u6210\u7684\u8bcd\u4e91\u56fe\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b8c\u6210\u8fd9\u4e00\u6b65\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud.to_file(&#39;wordcloud.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662f\u5236\u4f5c\u8bcd\u4e91\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\u7684\u5173\u952e\u70b9\u548c\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528WordCloud\u5e93\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u3002\u8fd9\u4e9b\u5e93\u5305\u62ecWordCloud\u3001matplotlib\u548cPillow\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\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>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fJupyter Notebook\uff0c\u53ef\u4ee5\u5728\u5355\u5143\u683c\u4e2d\u8fd0\u884c\u8fd9\u4e9b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install wordcloud matplotlib Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7684\u8d28\u91cf\u548c\u683c\u5f0f\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51b3\u5b9a\u4e86\u6700\u7ec8\u8bcd\u4e91\u56fe\u7684\u6548\u679c\u3002\u4f60\u53ef\u4ee5\u4ece\u591a\u79cd\u6765\u6e90\u5bfc\u5165\u6570\u636e\uff0c\u4f8b\u5982\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3002\u4e3a\u4e86\u7b80\u5316\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\u4f5c\u4e3a\u6570\u636e\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a&#39;sample.txt&#39;\u7684\u6587\u672c\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6587\u672c\u5185\u5bb9\uff0c\u4f8b\u5982\u65b0\u95fb\u6587\u7ae0\u3001\u535a\u5ba2\u5e16\u5b50\u6216\u4e66\u7c4d\u7ae0\u8282\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;sample.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u6574\u4e2a\u6587\u672c\u6587\u4ef6\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf<code>text<\/code>\u4e2d\u3002\u5982\u679c\u4f60\u7684\u6570\u636e\u6765\u6e90\u662fCSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;sample.csv&#39;)<\/p>\n<p>text = &#39; &#39;.join(df[&#39;column_name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u8bcd\u4e91\u56fe<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u8bcd\u4e91\u56fe\u662f\u6574\u4e2a\u8fc7\u7a0b\u7684\u6838\u5fc3\u90e8\u5206\u3002\u8fd9\u4e00\u6b65\u6d89\u53ca\u5230\u6570\u636e\u9884\u5904\u7406\u3001\u8bbe\u7f6e\u8bcd\u4e91\u56fe\u7684\u5f62\u72b6\u3001\u989c\u8272\u548c\u5176\u4ed6\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wordcloud import WordCloud, STOPWORDS<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u505c\u6b62\u8bcd<\/strong><\/h2>\n<p>stopwords = set(STOPWORDS)<\/p>\n<p>stopwords.update([&#39;example&#39;, &#39;word&#39;])<\/p>\n<h2><strong>\u751f\u6210\u8bcd\u4e91\u56fe<\/strong><\/h2>\n<p>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, min_font_size=10).generate(text)<\/p>\n<h2><strong>\u663e\u793a\u8bcd\u4e91\u56fe<\/strong><\/h2>\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><h4>\u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u6210\u8bcd\u4e91\u56fe\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u6570\u636e\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5220\u9664\u505c\u6b62\u8bcd\uff08\u5982\u201cthe\u201d\u3001\u201cand\u201d\u7b49\uff09\u53ef\u4ee5\u4f7f\u8bcd\u4e91\u56fe\u66f4\u52a0\u6e05\u6670\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528WordCloud\u5e93\u4e2d\u7684STOPWORDS\u96c6\u5408\u6765\u5b9a\u4e49\u505c\u6b62\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stopwords = set(STOPWORDS)<\/p>\n<p>stopwords.update([&#39;example&#39;, &#39;word&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u201cexample\u201d\u548c\u201cword\u201d\u5230\u9ed8\u8ba4\u7684\u505c\u6b62\u8bcd\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u8bcd\u4e91\u56fe\u53c2\u6570<\/h4>\n<\/p>\n<p><p>WordCloud\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u53c2\u6570\u6765\u5b9a\u5236\u8bcd\u4e91\u56fe\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u8bcd\u4e91\u56fe\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u80cc\u666f\u989c\u8272\u3001\u6700\u5c0f\u5b57\u4f53\u5927\u5c0f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, min_font_size=10).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u8bcd\u4e91\u56fe<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u7684\u8bcd\u4e91\u56fe\u53ef\u4ee5\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4fdd\u5b58\u8bcd\u4e91\u56fe\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud.to_file(&#39;wordcloud.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u8bcd\u4e91\u56fe\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\u7684\u56fe\u7247\u6587\u4ef6\u3002\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u683c\u5f0f\uff0c\u4f8b\u5982JPEG\u6216SVG\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud.to_file(&#39;wordcloud.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8fdb\u9636\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8bcd\u4e91\u56fe\u751f\u6210\u529f\u80fd\uff0cWordCloud\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u8fdb\u9636\u529f\u80fd\uff0c\u4f8b\u5982\u81ea\u5b9a\u4e49\u5f62\u72b6\u3001\u989c\u8272\u3001\u5b57\u4f53\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u5f62\u72b6<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u9ed1\u767d\u56fe\u7247\u4f5c\u4e3a\u8bcd\u4e91\u56fe\u7684\u5f62\u72b6\u6a21\u677f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u4e00\u5f20\u661f\u5f62\u56fe\u7247\u4f5c\u4e3a\u8bcd\u4e91\u56fe\u7684\u5f62\u72b6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<p>mask = np.array(Image.open(&#39;star.png&#39;))<\/p>\n<p>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, mask=mask).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><h4>\u81ea\u5b9a\u4e49\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u5b9a\u4e49\u6bcf\u4e2a\u5355\u8bcd\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u989c\u8272\u51fd\u6570\u6765\u8bbe\u7f6e\u6240\u6709\u5355\u8bcd\u4e3a\u7ea2\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def red_color_func(word, font_size, position, orientation, random_state=None, kwargs):<\/p>\n<p>    return &#39;rgb(255, 0, 0)&#39;<\/p>\n<p>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords).generate(text)<\/p>\n<p>wordcloud.recolor(color_func=red_color_func)<\/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><h4>\u81ea\u5b9a\u4e49\u5b57\u4f53<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55TTF\u683c\u5f0f\u7684\u5b57\u4f53\u6587\u4ef6\u6765\u751f\u6210\u8bcd\u4e91\u56fe\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u4e00\u6b3e\u81ea\u5b9a\u4e49\u5b57\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, font_path=&#39;custom_font.ttf&#39;).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><h3>\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>\u8bcd\u4e91\u56fe\u663e\u793a\u4e0d\u5b8c\u6574<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u751f\u6210\u7684\u8bcd\u4e91\u56fe\u663e\u793a\u4e0d\u5b8c\u6574\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u5355\u8bcd\u6570\u91cf\u8fc7\u591a\u6216\u5b57\u4f53\u5927\u5c0f\u8bbe\u7f6e\u4e0d\u5408\u7406\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u51cf\u5c11\u5355\u8bcd\u6570\u91cf\u6216\u8c03\u6574\u6700\u5c0f\u5b57\u4f53\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, max_words=100, min_font_size=10).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b57\u4f53\u6587\u4ef6\u627e\u4e0d\u5230<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53\u65f6\u9047\u5230\u5b57\u4f53\u6587\u4ef6\u627e\u4e0d\u5230\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u5e76\u4e14\u6587\u4ef6\u5b58\u5728\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords, font_path=&#39;path\/to\/custom_font.ttf&#39;).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u989c\u8272\u51fd\u6570\u4e0d\u751f\u6548<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u81ea\u5b9a\u4e49\u989c\u8272\u51fd\u6570\u65f6\u9047\u5230\u989c\u8272\u4e0d\u751f\u6548\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u989c\u8272\u51fd\u6570\u5b9a\u4e49\u6b63\u786e\u5e76\u4e14\u4f7f\u7528<code>recolor<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;, stopwords=stopwords).generate(text)<\/p>\n<p>wordcloud.recolor(color_func=red_color_func)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u751f\u6210\u5404\u79cd\u98ce\u683c\u548c\u5f62\u72b6\u7684\u8bcd\u4e91\u56fe\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3001\u62a5\u544a\u5c55\u793a\u8fd8\u662f\u521b\u610f\u8bbe\u8ba1\uff0c\u8bcd\u4e91\u56fe\u90fd\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u6613\u4e8e\u5b9e\u73b0\u7684\u5de5\u5177\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\u751f\u6210\u8bcd\u4e91\u56fe\uff1f<\/strong><br \/>\u5728\u751f\u6210\u8bcd\u4e91\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6587\u672c\u6570\u636e\u3002\u53ef\u4ee5\u9009\u62e9\u4ece\u4e66\u7c4d\u3001\u6587\u7ae0\u3001\u793e\u4ea4\u5a92\u4f53\u8bc4\u8bba\u6216\u5176\u4ed6\u6587\u672c\u6e90\u4e2d\u63d0\u53d6\u5185\u5bb9\u3002\u786e\u4fdd\u6240\u9009\u6570\u636e\u80fd\u53cd\u6620\u4f60\u60f3\u8981\u4f20\u8fbe\u7684\u4e3b\u9898\u6216\u60c5\u611f\uff0c\u8fd9\u6837\u751f\u6210\u7684\u8bcd\u4e91\u624d\u80fd\u66f4\u5177\u610f\u4e49\u548c\u5438\u5f15\u529b\u3002<\/p>\n<p><strong>\u751f\u6210\u8bcd\u4e91\u56fe\u9700\u8981\u54ea\u4e9bPython\u5e93\uff1f<\/strong><br \/>\u751f\u6210\u8bcd\u4e91\u56fe\u901a\u5e38\u9700\u8981\u4f7f\u7528\u51e0\u4e2aPython\u5e93\u3002\u6700\u5e38\u7528\u7684\u662f<code>wordcloud<\/code>\u5e93\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u8bcd\u4e91\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u7528\u5230<code>matplotlib<\/code>\u5e93\u6765\u5c55\u793a\u8bcd\u4e91\u56fe\uff0c<code>numpy<\/code>\u5e93\u53ef\u80fd\u4f1a\u5728\u5904\u7406\u6570\u636e\u65f6\u53d1\u6325\u4f5c\u7528\u3002\u786e\u4fdd\u5728\u4f60\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u4f8b\u5982\u53ef\u4ee5\u4f7f\u7528<code>pip install wordcloud matplotlib numpy<\/code>\u6765\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u8bcd\u4e91\u56fe\u7684\u5916\u89c2\u548c\u98ce\u683c\uff1f<\/strong><br \/>\u4f7f\u7528<code>wordcloud<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u8bcd\u4e91\u56fe\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u4f53\u3001\u80cc\u666f\u989c\u8272\u3001\u6700\u5927\u8bcd\u6570\u3001\u5355\u8bcd\u989c\u8272\u7b49\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u906e\u7f69\u56fe\u50cf\u521b\u5efa\u7279\u5b9a\u5f62\u72b6\u7684\u8bcd\u4e91\u3002\u901a\u8fc7\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u4f7f\u8bcd\u4e91\u56fe\u66f4\u7b26\u5408\u4e2a\u4eba\u6216\u54c1\u724c\u7684\u98ce\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5236\u4f5c\u8bcd\u4e91\u56fe\u6709\u5f88\u591a\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528WordCloud\u5e93\u3002\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u5bfc\u5165\u6570\u636e\u3001\u751f\u6210\u8bcd [&hellip;]","protected":false},"author":3,"featured_media":1061072,"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\/1061065"}],"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=1061065"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061065\/revisions"}],"predecessor-version":[{"id":1061074,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061065\/revisions\/1061074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061072"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}