Programming Language/Python
[Python] Wordcloud 만들기
draidev
2022. 3. 29. 17:10
[Python] konlpy 한국어 텍스트 분석과 시각화의 word_dict를 기반으로 wordcloud를 만들었습니다.
목차>
더보기
목차
wordcloud라이브러리 설치
- conda install -c https://conda.anaconda.org/conda-forge wordcloud==1.5.0
import wordcloud
from wordcloud import WordCloud
from PIL import Image # 만약 "No module named 'PIL'" 에러가 발생하면 [ pip install Pillow==5.4.1 ] 로 라이브러리를 설치
import numpy as np
import matplotlib.pyplot as plt
01
WordCloud객체변수 word_cloud를 정의해서 출력해보면 데이터타입이 출력됩니다.
- <wordcloud.wordcloud.WordCloud object at 0x000001D18D97E610>
generate_from_frequencies( ) 함수에 딕셔너리를 전달인자로 사용하면 value값에 따라 글자의 크기가 달라지게 됩니다.
이때도 타입은 아래와 같이 WordCloud객체입니다.
- <wordcloud.wordcloud.WordCloud object at 0x000001BD8FA4C610>
word_cloud = WordCloud(font_path='C:/Windows/Fonts/malgun.ttf',
width = 2000, height = 1000,
background_color='white')
word_cloud.generate_from_frequencies(word_dic)
plt.figure(figsize=(15,15))
plt.imshow(word_cloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
02 폰트 사이즈, 단어 갯수, 바탕 설정
WordCloud클래스를 정의할 때 파라미터들을 조절합니다.
- max_words
- max_font_size
- background_color (default == 'black')
word_cloud = WordCloud(font_path="C:/Windows/Fonts/malgun.ttf", # font_path="C:/Windows/Fonts/NanumSquareB.ttf"
max_words=50, # max words,
width=2000, height=1000,
#background_color='white',
max_font_size=100).generate_from_frequencies(word_dic) # Max font-size
plt.figure(figsize=(15,15))
plt.imshow(word_cloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
03-01 WordCloud - masking 적용
- masking을 하기위한 이미지 파일을 준비하고 (해상도가 좋은 이미지가 글씨가 깔끔하게 나옵니다.)
- PIL라이브러리의 Image.open메서드를 사용해서 열어줍니다.
- np.array()로 numpy행렬로 변환합니다.
- WordCloud( ) 생성자의 mask파라미터에 행렬 변수를 전달합니다.
masking_image = np.array(Image.open(".\images\\apple1.jpg"))
word_cloud = WordCloud(font_path="C:/Windows/Fonts/malgun.ttf", # font_path="C:/Windows/Fonts/NanumSquareB.ttf"
width=2000, height=1000,
mask=masking_image, # masking
background_color='white').generate_from_frequencies(word_dic)
plt.figure(figsize=(15,15))
plt.imshow(word_cloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
03_02 WordCloud - masking (이미지의 색상으로 만들기)
- from wordcloud import ImageColorGenerator
- ImageColorGenerator( )
- recolor(color_func=) : ImageColorGenerator로 생성한 이미지행렬로 word_cloud를 변환
- interpolation = 'bilinear' (Bilinear Interpolation : 쌍선형 보간법)
(참고 : 선형보간법wiki , 블로그 : https://darkpgmr.tistory.com/117)
from wordcloud import ImageColorGenerator
masking_image = np.array(Image.open(".\images\\apple1.jpg"))
word_cloud = WordCloud(font_path="C:/Windows/Fonts/malgun.ttf", # font_path="C:/Windows/Fonts/NanumSquareB.ttf"
width=2000, height=1000,
mask=masking_image, # masking
background_color='white').generate_from_frequencies(word_dic)
image_colors = ImageColorGenerator(masking_image)
word_cloud = word_cloud.recolor(color_func=image_colors)
word_cloud.to_file(filename='Apple.jpg') # 파일로 저장
plt.figure(figsize=(15,15))
plt.imshow(word_cloud, interpolation='bilinear')
# plt.imshow(word_cloud.recolor(colormap='Blues'), interpolation='bilinear') # Matplotlib colormap 활용 (http://j.mp/32UXOQ6)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()