{"id":1061781,"date":"2024-12-31T15:45:44","date_gmt":"2024-12-31T07:45:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061781.html"},"modified":"2024-12-31T15:45:46","modified_gmt":"2024-12-31T07:45:46","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python3%e5%88%b6%e4%bd%9c%e8%af%8d%e4%ba%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061781.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python3\u5236\u4f5c\u8bcd\u4e91"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0f9fb5ac-a0a8-4c9a-8722-197b71d6964b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u4f7f\u7528python3\u5236\u4f5c\u8bcd\u4e91\" \/><\/p>\n<p><p> <strong>\u56de\u7b54\u6807\u9898\u6240\u63d0\u95ee\u9898<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python3\u5236\u4f5c\u8bcd\u4e91\u7684\u6b65\u9aa4\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<strong>\u5b89\u88c5\u6240\u9700\u5e93\u3001\u52a0\u8f7d\u6587\u672c\u6570\u636e\u3001\u751f\u6210\u8bcd\u9891\u3001\u914d\u7f6e\u8bcd\u4e91\u53c2\u6570\u3001\u751f\u6210\u8bcd\u4e91\u56fe\u50cf<\/strong>\u3002\u5176\u4e2d\uff0c\u5b89\u88c5\u6240\u9700\u5e93\u662f\u5236\u4f5c\u8bcd\u4e91\u7684\u7b2c\u4e00\u6b65\uff0c\u5177\u4f53\u9700\u8981\u5b89\u88c5\u7684\u5e93\u5305\u62ec<code>wordcloud<\/code>\u3001<code>matplotlib<\/code>\u3001\u548c<code>numpy<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u6784\u5efa\u548c\u663e\u793a\u8bcd\u4e91\u7684\u57fa\u7840\u5de5\u5177\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u4e2a\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u8bcd\u4e91\u5236\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5236\u4f5c\u8bcd\u4e91\u4e4b\u524d\uff0c\u5fc5\u987b\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u8fd9\u4e9b\u5e93\u5305\u62ec<code>wordcloud<\/code>\u3001<code>matplotlib<\/code>\u548c<code>numpy<\/code>\u3002<code>wordcloud<\/code>\u5e93\u7528\u4e8e\u751f\u6210\u8bcd\u4e91\uff0c<code>matplotlib<\/code>\u5e93\u7528\u4e8e\u7ed8\u5236\u548c\u663e\u793a\u751f\u6210\u7684\u8bcd\u4e91\u56fe\u50cf\uff0c\u800c<code>numpy<\/code>\u5e93\u5219\u7528\u4e8e\u5904\u7406\u548c\u7ba1\u7406\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wordcloud matplotlib numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u6240\u6709\u5e93\u90fd\u6210\u529f\u5b89\u88c5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e8c\u3001\u52a0\u8f7d\u6587\u672c\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u8bcd\u4e91\u7684\u751f\u6210\u4f9d\u8d56\u4e8e\u8f93\u5165\u6587\u672c\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u5f62\u5f0f\u7684\u6587\u672c\u6570\u636e\u6765\u751f\u6210\u8bcd\u4e91\uff0c\u4f8b\u5982\u4e66\u7c4d\u3001\u6587\u7ae0\u3001\u793e\u4ea4\u5a92\u4f53\u5e16\u5b50\u7b49\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\u4e3a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u52a0\u8f7d\u548c\u5904\u7406\u6587\u672c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<p>from wordcloud import WordCloud<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;sample_text.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>open<\/code>\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>text<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e09\u3001\u751f\u6210\u8bcd\u9891<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210\u8bcd\u4e91\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u8f93\u5165\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u8bcd\u9891\u3002\u8fd9\u4e00\u6b65\u901a\u5e38\u662f\u7531<code>wordcloud<\/code>\u5e93\u81ea\u52a8\u5b8c\u6210\u7684\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u624b\u52a8\u8ba1\u7b97\u8bcd\u9891\u5e76\u4f20\u9012\u7ed9<code>WordCloud<\/code>\u7c7b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>wordcloud<\/code>\u5e93\u6765\u81ea\u52a8\u751f\u6210\u8bcd\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaWordCloud\u5bf9\u8c61<\/p>\n<p>wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>WordCloud<\/code>\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u4e86\u56fe\u50cf\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u548c\u80cc\u666f\u989c\u8272\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>generate<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u6587\u672c\u8f6c\u5316\u4e3a\u8bcd\u4e91\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u56db\u3001\u914d\u7f6e\u8bcd\u4e91\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u8bcd\u4e91\u65f6\uff0c\u6709\u8bb8\u591a\u53c2\u6570\u53ef\u4ee5\u914d\u7f6e\uff0c\u4ee5\u4f18\u5316\u8bcd\u4e91\u7684\u5916\u89c2\u548c\u6548\u679c\u3002\u8fd9\u4e9b\u53c2\u6570\u5305\u62ec\u56fe\u50cf\u5927\u5c0f\u3001\u80cc\u666f\u989c\u8272\u3001\u6700\u5927\u8bcd\u6570\u3001\u5b57\u4f53\u8def\u5f84\u3001\u989c\u8272\u6620\u5c04\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u53c2\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    max_words=200,<\/p>\n<p>    colormap=&#39;viridis&#39;,<\/p>\n<p>    contour_color=&#39;steelblue&#39;,<\/p>\n<p>    contour_width=1<\/p>\n<p>).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u8bcd\u4e91\u7684\u6700\u5927\u8bcd\u6570\u4e3a200\uff0c\u5e76\u4f7f\u7528<code>viridis<\/code>\u989c\u8272\u6620\u5c04\u548c\u84dd\u8272\u8f6e\u5ed3\u7ebf\u3002\u901a\u8fc7\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u66f4\u7b26\u5408\u9700\u6c42\u7684\u8bcd\u4e91\u56fe\u50cf\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e94\u3001\u751f\u6210\u8bcd\u4e91\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u751f\u6210\u5e76\u663e\u793a\u8bcd\u4e91\u56fe\u50cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u6765\u7ed8\u5236\u548c\u5c55\u793a\u8bcd\u4e91\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u8bcd\u4e91\u56fe\u50cf<\/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;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>imshow<\/code>\u65b9\u6cd5\u5c06\u8bcd\u4e91\u56fe\u50cf\u7ed8\u5236\u5728\u56fe\u5f62\u7a97\u53e3\u4e2d\uff0c\u5e76\u4f7f\u7528<code>axis(&#39;off&#39;)<\/code>\u6765\u5173\u95ed\u5750\u6807\u8f74\uff0c\u4f7f\u56fe\u50cf\u66f4\u52a0\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u516d\u3001\u4fdd\u5b58\u8bcd\u4e91\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u663e\u793a\u8bcd\u4e91\u56fe\u50cf\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff0c\u4ee5\u4fbf\u65e5\u540e\u67e5\u770b\u6216\u5206\u4eab\u3002\u53ef\u4ee5\u4f7f\u7528<code>WordCloud<\/code>\u5bf9\u8c61\u7684<code>to_file<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u8bcd\u4e91\u56fe\u50cf<\/p>\n<p>wordcloud.to_file(&#39;wordcloud.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u8bcd\u4e91\u56fe\u50cf\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>wordcloud.png<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\u548c\u8499\u7248<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u8bcd\u4e91\u56fe\u50cf\u66f4\u52a0\u72ec\u7279\u548c\u6709\u8da3\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\u548c\u8499\u7248\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u8499\u7248\u56fe\u50cf\u6765\u751f\u6210\u7279\u5b9a\u5f62\u72b6\u7684\u8bcd\u4e91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u52a0\u8f7d\u8499\u7248\u56fe\u50cf<\/strong><\/h2>\n<p>mask = np.array(Image.open(&#39;mask.png&#39;))<\/p>\n<h2><strong>\u521b\u5efaWordCloud\u5bf9\u8c61\uff0c\u5e76\u5e94\u7528\u8499\u7248<\/strong><\/h2>\n<p>wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    mask=mask,<\/p>\n<p>    contour_color=&#39;steelblue&#39;,<\/p>\n<p>    contour_width=1<\/p>\n<p>).generate(text)<\/p>\n<h2><strong>\u7ed8\u5236\u8bcd\u4e91\u56fe\u50cf<\/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><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>PIL<\/code>\u5e93\u52a0\u8f7d\u8499\u7248\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a<code>numpy<\/code>\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u521b\u5efa<code>WordCloud<\/code>\u5bf9\u8c61\u65f6\u5e94\u7528\u8499\u7248\u56fe\u50cf\uff0c\u4ece\u800c\u751f\u6210\u5177\u6709\u7279\u5b9a\u5f62\u72b6\u7684\u8bcd\u4e91\u56fe\u50cf\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u516b\u3001\u5904\u7406\u505c\u7528\u8bcd<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210\u8bcd\u4e91\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8fc7\u6ee4\u6389\u4e00\u4e9b\u5e38\u89c1\u7684\u65e0\u610f\u4e49\u5355\u8bcd\uff08\u5982\u201cthe\u201d\u3001\u201cand\u201d\u3001\u201cis\u201d\u7b49\uff09\uff0c\u8fd9\u4e9b\u5355\u8bcd\u88ab\u79f0\u4e3a\u505c\u7528\u8bcd\u3002\u53ef\u4ee5\u4f7f\u7528<code>WordCloud<\/code>\u5e93\u7684<code>stopwords<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u505c\u7528\u8bcd\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wordcloud import STOPWORDS<\/p>\n<h2><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u505c\u7528\u8bcd<\/strong><\/h2>\n<p>stopwords = set(STOPWORDS)<\/p>\n<p>stopwords.update([&#39;example&#39;, &#39;custom&#39;, &#39;words&#39;])<\/p>\n<h2><strong>\u521b\u5efaWordCloud\u5bf9\u8c61\uff0c\u5e76\u5e94\u7528\u505c\u7528\u8bcd<\/strong><\/h2>\n<p>wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    stopwords=stopwords,<\/p>\n<p>    max_words=200<\/p>\n<p>).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ece<code>wordcloud<\/code>\u5e93\u4e2d\u5bfc\u5165\u9ed8\u8ba4\u7684\u505c\u7528\u8bcd\u5217\u8868\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u505c\u7528\u8bcd\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u521b\u5efa<code>WordCloud<\/code>\u5bf9\u8c61\u65f6\u5e94\u7528\u505c\u7528\u8bcd\u5217\u8868\uff0c\u4ece\u800c\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5355\u8bcd\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e5d\u3001\u8c03\u6574\u8bcd\u4e91\u751f\u6210\u7684\u5176\u4ed6\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u89c1\u7684\u53c2\u6570\u5916\uff0c<code>WordCloud<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u8c03\u6574\u8bcd\u4e91\u7684\u751f\u6210\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8bcd\u4e91\u7684\u6700\u5927\u5b57\u4f53\u5927\u5c0f\u3001\u6700\u5c0f\u5b57\u4f53\u5927\u5c0f\u3001\u8bcd\u4e0e\u8bcd\u4e4b\u95f4\u7684\u95f4\u8ddd\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    max_words=200,<\/p>\n<p>    max_font_size=100,<\/p>\n<p>    min_font_size=10,<\/p>\n<p>    relative_scaling=0.5<\/p>\n<p>).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u8bcd\u4e91\u7684\u6700\u5927\u5b57\u4f53\u5927\u5c0f\u4e3a100\uff0c\u6700\u5c0f\u5b57\u4f53\u5927\u5c0f\u4e3a10\uff0c\u5e76\u8c03\u6574\u4e86\u8bcd\u4e0e\u8bcd\u4e4b\u95f4\u7684\u76f8\u5bf9\u7f29\u653e\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u5341\u3001\u4f7f\u7528\u591a\u79cd\u5b57\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u8bcd\u4e91\u66f4\u52a0\u4e30\u5bcc\u591a\u5f69\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5b57\u4f53\u3002\u53ef\u4ee5\u901a\u8fc7<code>WordCloud<\/code>\u7c7b\u7684<code>font_path<\/code>\u53c2\u6570\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    max_words=200,<\/p>\n<p>    font_path=&#39;path\/to\/font.ttf&#39;<\/p>\n<p>).generate(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u4f53\u6587\u4ef6\uff0c\u4ece\u800c\u4f7f\u8bcd\u4e91\u4e2d\u7684\u6587\u5b57\u4ee5\u8be5\u5b57\u4f53\u663e\u793a\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u5341\u4e00\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5206\u6790\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u8bcd\u4e91\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6570\u636e\u5206\u6790\u5de5\u5177\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u83b7\u5f97\u66f4\u6df1\u5165\u7684\u89c1\u89e3\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u7136\u540e\u751f\u6210\u8bcd\u4e91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u751f\u6210\u6587\u672c\u6570\u636e<\/strong><\/h2>\n<p>text = &#39; &#39;.join(df[&#39;text_column&#39;])<\/p>\n<h2><strong>\u521b\u5efaWordCloud\u5bf9\u8c61<\/strong><\/h2>\n<p>wordcloud = WordCloud(<\/p>\n<p>    width=800,<\/p>\n<p>    height=400,<\/p>\n<p>    background_color=&#39;white&#39;,<\/p>\n<p>    max_words=200<\/p>\n<p>).generate(text)<\/p>\n<h2><strong>\u7ed8\u5236\u8bcd\u4e91\u56fe\u50cf<\/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><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u6307\u5b9a\u5217\u7684\u6587\u672c\u6570\u636e\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u751f\u6210\u8bcd\u4e91\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u5341\u4e8c\u3001\u52a8\u6001\u66f4\u65b0\u8bcd\u4e91<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u66f4\u65b0\u8bcd\u4e91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u5b9e\u65f6\u751f\u6210\u548c\u66f4\u65b0\u8bcd\u4e91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>IPython.display<\/code>\u6a21\u5757\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from IPython.display import display, clear_output<\/p>\n<h2><strong>\u52a8\u6001\u66f4\u65b0\u8bcd\u4e91\u51fd\u6570<\/strong><\/h2>\n<p>def update_wordcloud(text):<\/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>    clear_output(w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t=True)<\/p>\n<p>    display(plt.gcf())<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c\u6570\u636e<\/strong><\/h2>\n<p>text_data = [&quot;This is the first example text.&quot;, &quot;Here is another piece of text.&quot;, &quot;Word clouds are fun!&quot;]<\/p>\n<h2><strong>\u52a8\u6001\u751f\u6210\u8bcd\u4e91<\/strong><\/h2>\n<p>for text in text_data:<\/p>\n<p>    update_wordcloud(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u52a8\u6001\u66f4\u65b0\u8bcd\u4e91\u7684\u51fd\u6570<code>update_wordcloud<\/code>\uff0c\u5e76\u4f7f\u7528<code>IPython.display<\/code>\u6a21\u5757\u6e05\u9664\u4e4b\u524d\u7684\u8f93\u51fa\u5e76\u663e\u793a\u65b0\u7684\u8bcd\u4e91\u56fe\u50cf\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u5341\u4e09\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python3\u5236\u4f5c\u51fa\u5404\u79cd\u6837\u5f0f\u548c\u5f62\u72b6\u7684\u8bcd\u4e91\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u6240\u9700\u5e93\u3001\u52a0\u8f7d\u6587\u672c\u6570\u636e\u3001\u751f\u6210\u8bcd\u9891\u3001\u914d\u7f6e\u8bcd\u4e91\u53c2\u6570\u3001\u751f\u6210\u548c\u663e\u793a\u8bcd\u4e91\u56fe\u50cf\u3001\u4fdd\u5b58\u56fe\u50cf\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f62\u72b6\u548c\u8499\u7248\u3001\u5904\u7406\u505c\u7528\u8bcd\u3001\u8c03\u6574\u751f\u6210\u53c2\u6570\u3001\u4f7f\u7528\u591a\u79cd\u5b57\u4f53\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5206\u6790\u5de5\u5177\u3001\u4ee5\u53ca\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0\u3002\u901a\u8fc7\u4e0d\u65ad\u8c03\u6574\u548c\u4f18\u5316\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u66f4\u52a0\u7b26\u5408\u9700\u6c42\u548c\u7f8e\u89c2\u7684\u8bcd\u4e91\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u7406\u89e3\u548c\u638c\u63e1\u5982\u4f55\u4f7f\u7528Python3\u5236\u4f5c\u8bcd\u4e91\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u5236\u4f5c\u8bcd\u4e91\u6240\u9700\u7684\u5e93\uff1f<\/strong><br \/>\u8981\u5236\u4f5c\u8bcd\u4e91\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u51e0\u4e2aPython\u5e93\u3002\u6700\u91cd\u8981\u7684\u5e93\u662f<code>wordcloud<\/code>\uff0c\u60a8\u8fd8\u9700\u8981<code>matplotlib<\/code>\u6765\u663e\u793a\u751f\u6210\u7684\u8bcd\u4e91\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install wordcloud matplotlib\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5f00\u59cb\u5236\u4f5c\u8bcd\u4e91\u4e86\u3002<\/p>\n<p><strong>\u5236\u4f5c\u8bcd\u4e91\u9700\u8981\u54ea\u4e9b\u8f93\u5165\u6570\u636e\uff1f<\/strong><br \/>\u5236\u4f5c\u8bcd\u4e91\u7684\u8f93\u5165\u6570\u636e\u901a\u5e38\u662f\u4e00\u6bb5\u6587\u672c\u3002\u8fd9\u53ef\u4ee5\u662f\u4efb\u4f55\u5f62\u5f0f\u7684\u6587\u672c\uff0c\u5982\u6587\u7ae0\u3001\u8bc4\u8bba\u6216\u793e\u4ea4\u5a92\u4f53\u5e16\u5b50\u3002\u786e\u4fdd\u6587\u672c\u8db3\u591f\u4e30\u5bcc\uff0c\u4ee5\u4fbf\u8bcd\u4e91\u80fd\u591f\u53cd\u6620\u51fa\u6587\u672c\u4e2d\u6700\u5e38\u7528\u7684\u8bcd\u6c47\u3002\u5e38\u89c1\u7684\u6587\u672c\u6765\u6e90\u8fd8\u5305\u62ecCSV\u6587\u4ef6\u3001\u6587\u672c\u6587\u4ef6\u6216\u76f4\u63a5\u4ece\u7f51\u7edc\u6293\u53d6\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u8bcd\u4e91\u7684\u5916\u89c2\u548c\u5f62\u72b6\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>WordCloud<\/code>\u7c7b\u7684\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u8bcd\u4e91\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u3001\u6700\u5927\u8bcd\u6570\u3001\u5b57\u4f53\u5927\u5c0f\u3001\u5f62\u72b6\u7b49\u3002\u82e5\u8981\u5b9e\u73b0\u7279\u5b9a\u7684\u5f62\u72b6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8499\u7248\u56fe\u50cf\u3002\u53ea\u9700\u5728\u521b\u5efa\u8bcd\u4e91\u65f6\u4f20\u5165\u8499\u7248\u56fe\u50cf\u8def\u5f84\uff0c\u5e76\u8bbe\u7f6e<code>mask<\/code>\u53c2\u6570\uff0c\u60a8\u5c31\u80fd\u5f97\u5230\u72ec\u7279\u98ce\u683c\u7684\u8bcd\u4e91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u56de\u7b54\u6807\u9898\u6240\u63d0\u95ee\u9898\uff1a \u4f7f\u7528Python3\u5236\u4f5c\u8bcd\u4e91\u7684\u6b65\u9aa4\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u70b9\uff1a\u5b89\u88c5\u6240\u9700\u5e93\u3001\u52a0\u8f7d\u6587\u672c\u6570\u636e\u3001\u751f\u6210\u8bcd\u9891\u3001\u914d [&hellip;]","protected":false},"author":3,"featured_media":1061792,"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\/1061781"}],"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=1061781"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061781\/revisions"}],"predecessor-version":[{"id":1061794,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061781\/revisions\/1061794"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061792"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}