Python/Python For Analytics

[Python] wordcloud 만들기

Pydole 2021. 1. 28. 23:28

 

라이브러리 설치

 

pip install wordcloud
pip install matplotlib

 

 

 

 

텍스트의 빈도를 계산하여 클라우드 그리기

 

 

text = '''
파이썬 파이썬 자바 파이썬 C언어 GOLANG 파이썬 ASP 자바스크립트
파이썬 C++ C언어 델파이 파이썬
'''

from wordcloud import WordCloud
import matplotlib.pyplot as plt

wordcloud = WordCloud(font_path='NanumBarunGothic.ttf',
                      background_color='black').generate_from_text(text)

plt.figure(figsize=(5,5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

 

 

 

딕셔너리를 이용하여 카운트를 이용하여 그리기

 

text = {'파이썬':5,'자바':3,'C언어':4,'자바스크립트':2,'델파이':1,'GOLANG':1}

from wordcloud import WordCloud
import matplotlib.pyplot as plt

wordcloud = WordCloud(font_path='NanumBarunGothic.ttf',
                      background_color='white').generate_from_frequencies(text)

plt.figure(figsize=(5,5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

 

 

 

 

불용어 처리

 

text = '''
파이썬 파이썬 자바 파이썬 C언어 GOLANG 파이썬 ASP 자바스크립트
파이썬 C++ C언어 델파이 파이썬
'''

from wordcloud import WordCloud
from wordcloud import STOPWORDS
import matplotlib.pyplot as plt

stopwords = set(STOPWORDS)
stopwords.add('자바')

wordcloud = WordCloud(font_path='NanumBarunGothic.ttf',
                      background_color='white',
                      stopwords=stopwords).generate_from_text(text)

plt.figure(figsize=(5,5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()