예제 데이터는 데이콘 대회 중 '감귤 착과량 예측 AI경진대회'의 train 데이터셋 이용
In [1]:
import pandas as pd
gyul_train_df = pd.read_csv('./jeju_mandarin/train.csv')
In [2]:
gyul_growth = gyul_train_df[['착과량(int)', '수고(m)', '수관폭1(min)', '수관폭2(max)', '수관폭평균']]
gyul_growth
Out[2]:
착과량(int) | 수고(m) | 수관폭1(min) | 수관폭2(max) | 수관폭평균 | |
---|---|---|---|---|---|
0 | 692 | 275.0 | 287.0 | 292.0 | 289.5 |
1 | 534 | 293.0 | 284.0 | 336.0 | 310.0 |
2 | 634 | 300.0 | 392.0 | 450.0 | 421.0 |
3 | 639 | 289.0 | 368.0 | 379.0 | 373.5 |
4 | 496 | 306.0 | 353.0 | 358.0 | 355.5 |
... | ... | ... | ... | ... | ... |
2202 | 119 | 230.0 | 353.0 | 367.0 | 360.0 |
2203 | 611 | 266.0 | 337.0 | 400.0 | 368.5 |
2204 | 650 | 237.0 | 353.0 | 362.0 | 357.5 |
2205 | 363 | 281.0 | 337.0 | 403.0 | 370.0 |
2206 | 17 | 228.0 | 389.0 | 398.0 | 393.5 |
2207 rows × 5 columns
- 칼럼명이 다 한글임
In [5]:
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
sns.heatmap(gyul_growth.corr(), cmap='Greens', annot=True)
warnings.filterwarnings(action='ignore')
- 한글이 깨져 나오는 상황 발생
이미 설치된 한글 폰트 지정해주기
- 나눔고딕이나 맑은 고딕 폰트가 설치되어 있어야 아래와 같은 방법이 통함
In [6]:
#Window OS
plt.rcParams['font.family'] = 'NanumGothic'
plt.rcParams['font.family'] = 'Malgun Gothic'
In [7]:
#Mac OS
plt.rcParams['font.family'] = 'AppleGothic'
다른 한글 폰트를 사용하고 싶은 경우
In [9]:
#이미 설치된 한글 폰트가 뭐가 있는지 출력
import matplotlib.font_manager as fm
# 설치된 폰트 출력
font_list = [font.name for font in fm.fontManager.ttflist]
font_list
Out[9]:
['cmr10',
'STIXNonUnicode',
'STIXSizeFourSym',
'STIXGeneral',
'STIXSizeFourSym',
'STIXSizeTwoSym',
'DejaVu Sans Mono',
'DejaVu Serif Display',
'DejaVu Sans',
'STIXSizeFiveSym',
'cmtt10',
'cmss10',
'cmmi10',
'DejaVu Sans',
'STIXSizeOneSym',
'DejaVu Sans',
'DejaVu Serif',
'STIXNonUnicode',
'cmex10',
'STIXSizeThreeSym',
'STIXGeneral',
'DejaVu Sans Mono',
'STIXSizeThreeSym',
'STIXNonUnicode',
'DejaVu Sans Display',
'STIXNonUnicode',
'DejaVu Sans',
'cmsy10',
'DejaVu Serif',
'DejaVu Serif',
'STIXGeneral',
'STIXSizeOneSym',
'STIXGeneral',
'STIXSizeTwoSym',
'DejaVu Sans Mono',
'DejaVu Serif',
'cmb10',
'DejaVu Sans Mono',
'Bookshelf Symbol 7',
'Poor Richard',
'Wingdings 2',
'STXinwei',
'Segoe UI',
'Century Gothic',
'Calisto MT',
'Verdana',
'Parchment',
'Perpetua',
'Forte',
'Mongolian Baiti',
'Goudy Old Style',
'Goudy Old Style',
'Lucida Handwriting',
'Webdings',
'Lucida Bright',
'Corbel',
'Perpetua Titling MT',
'Ink Free',
'Yu Gothic',
'HYHeadLine-Medium',
'Eras Bold ITC',
'Arial',
'Pristina',
'Script MT Bold',
'Cambria',
'Georgia',
'Courier New',
'Colonna MT',
'Lucida Sans',
'NanumGothic',
'Snap ITC',
'STXingkai',
'STLiti',
'NanumGothic',
'Segoe UI Symbol',
'Showcard Gothic',
'Franklin Gothic Heavy',
'Gloucester MT Extra Condensed',
'Malgun Gothic',
'NanumGothic',
'Magic R',
'Calibri',
'Yu Gothic',
'Segoe UI',
'ZWAdobeF',
'Berlin Sans FB Demi',
'Perpetua',
'Microsoft Tai Le',
'Calisto MT',
'Calibri',
'Century Gothic',
'Goudy Stout',
'Wingdings',
'Niagara Solid',
'Onyx',
'Franklin Gothic Medium',
'Franklin Gothic Demi',
'Segoe UI',
'Microsoft Tai Le',
'HYGothic-Medium',
'Lucida Bright',
'HYGraphic-Medium',
'Malgun Gothic',
'Eras Light ITC',
'Franklin Gothic Heavy',
'Tw Cen MT Condensed',
'Bahnschrift',
'Lucida Sans Typewriter',
'Comic Sans MS',
'HCR Batang ExtB',
'Brush Script MT',
'Tw Cen MT',
'Trebuchet MS',
'Blackadder ITC',
'Segoe UI Historic',
'Lucida Sans Unicode',
'Arial Rounded MT Bold',
'Georgia',
'Bell MT',
'Book Antiqua',
'Microsoft JhengHei',
'Bell MT',
'Elephant',
'Harlow Solid Italic',
'Bradley Hand ITC',
'Tw Cen MT Condensed',
'Segoe UI',
'Pyidaungsu',
'Yet R',
'Dubai',
'Segoe UI',
'Gill Sans MT Condensed',
'Times New Roman',
'Maiandra GD',
'HCR Batang Ext',
'Elephant',
'Rockwell',
'Headline R',
'MS Reference Specialty',
'Microsoft JhengHei',
'Binggrae',
'Segoe UI',
'FZYaoTi',
'Malgun Gothic',
'Pyidaungsu Numbers',
'Gill Sans Ultra Bold Condensed',
'Gulim',
'Bodoni MT',
'MingLiU-ExtB',
'Lucida Console',
'Franklin Gothic Book',
'Arial',
'Franklin Gothic Book',
'Leelawadee UI',
'Segoe MDL2 Assets',
'Agency FB',
'Wide Latin',
'Lucida Sans Typewriter',
'Lucida Sans Typewriter',
'Century Gothic',
'Century',
'Felix Titling',
'Arial',
'Segoe UI',
'Courier New',
'Britannic Bold',
'Stencil',
'Microsoft PhagsPa',
'Playbill',
'Tw Cen MT',
'Arial',
'Tempus Sans ITC',
'HYGungSo-Bold',
'Segoe UI',
'Papyrus',
'Phetsarath OT',
'NewJumja',
'HCR Batang ExtB',
'V3Detect',
'Verdana',
'Nirmala UI',
'Arial',
'HCR Batang',
'Berlin Sans FB',
'Gill Sans MT',
'HCR Dotum Ext',
'Rockwell',
'Segoe UI',
'Lucida Sans Typewriter',
'Berlin Sans FB',
'Cambria',
'Corbel',
'HYMyeongJo-Extra',
'FZShuTi',
'Dubai',
'Trebuchet MS',
'Garamond',
'Segoe Print',
'Segoe Fluent Icons',
'HYSinMyeongJo-Medium',
'Constantia',
'HYShortSamul-Medium',
'Franklin Gothic Demi Cond',
'Calibri',
'Californian FB',
'Courier New',
'Palatino Linotype',
'Tahoma',
'Consolas',
'Candara',
'Ebrima',
'Microsoft Yi Baiti',
'Old English Text MT',
'Times New Roman',
'Goudy Old Style',
'Gigi',
'Hancom Hoonminjeongeum_V',
'Constantia',
'Bodoni MT',
'Footlight MT Light',
'New Gulim',
'Century Gothic',
'Myanmar Text',
'Cambria',
'STKaiti',
'Ebrima',
'Corbel',
'Arial',
'Imprint MT Shadow',
'STXihei',
'Ravie',
'Candara',
'Calibri',
'Vladimir Script',
'Juice ITC',
'Century Schoolbook',
'Hancom MalangMalang',
'Perpetua',
'Cooper Black',
'Freestyle Script',
'Gill Sans MT',
'Microsoft YaHei',
'Gill Sans MT',
'Bodoni MT',
'Leelawadee UI',
'Comic Sans MS',
'Nirmala UI',
'Han Santteut Dotum',
'Bookman Old Style',
'STZhongsong',
'Times New Roman',
'Lucida Sans',
'Californian FB',
'Tahoma',
'HYPMokGak-Bold',
'High Tower Text',
'Copperplate Gothic Light',
'Lucida Sans',
'Microsoft Himalaya',
'Bookman Old Style',
'Book Antiqua',
'Courier New',
'Sylfaen',
'Centaur',
'Impact',
'Consolas',
'Lucida Fax',
'Bell MT',
'MV Boli',
'Wingdings 3',
'Gill Sans MT',
'Castellar',
'Algerian',
'Perpetua',
'Bodoni MT',
'Kunstler Script',
'French Script MT',
'Segoe UI',
'Javanese Text',
'Pyidaungsu',
'Times New Roman',
'Trebuchet MS',
'Bodoni MT',
'Tw Cen MT Condensed Extra Bold',
'Candara',
'Copperplate Gothic Bold',
'Yu Gothic',
'Verdana',
'Vivaldi',
'Cambria',
'Comic Sans MS',
'Bernard MT Condensed',
'Agency FB',
'Palace Script MT',
'Rockwell Condensed',
'Candara',
'Binggrae?',
'Lucida Calligraphy',
'Consolas',
'Microsoft New Tai Lue',
'Haettenschweiler',
'Arial',
'Calibri',
'HYGothic-Extra',
'Dubai',
'Symbol',
'STCaiyun',
'Bodoni MT',
'Microsoft Sans Serif',
'Franklin Gothic Demi',
'Curlz MT',
'MoeumT R',
'MT Extra',
'Constantia',
'Binggrae?',
'Magneto',
'Han Santteut Dotum',
'Eras Demi ITC',
'Viner Hand ITC',
'Lucida Bright',
'Lucida Bright',
'YouYuan',
'Segoe UI',
'Haan Wing2',
'Ami R',
'MS Reference Sans Serif',
'Segoe Script',
'Rockwell Condensed',
'Consolas',
'Segoe Script',
'Microsoft PhagsPa',
'HYPost-Light',
'STHupo',
'Perpetua Titling MT',
'Lucida Fax',
'Palatino Linotype',
'Niagara Engraved',
'Segoe UI Emoji',
'Constantia',
'Microsoft YaHei',
'SimSun',
'Tw Cen MT',
'Georgia',
'Bodoni MT',
'HCR Dotum',
'Calisto MT',
'Batang',
'Verdana',
'Century Schoolbook',
'STSong',
'Informal Roman',
'Lucida Fax',
'Eras Medium ITC',
'Garamond',
'Trebuchet MS',
'STFangsong',
'Harrington',
'SimSun-ExtB',
'Calibri',
'Gadugi',
'Leelawadee UI',
'Corbel',
'Gill Sans Ultra Bold',
'Bodoni MT',
'Californian FB',
'Franklin Gothic Medium Cond',
'Bookman Old Style',
'Baskerville Old Face',
'HCR Batang',
'Sitka',
'Rage Italic',
'Hancom Gothic',
'Rockwell',
'Calisto MT',
'Monotype Corsiva',
'Century Schoolbook',
'Segoe Print',
'Bodoni MT',
'Lucida Fax',
'Chiller',
'OCR A Extended',
'Bodoni MT',
'Microsoft JhengHei',
'MS Outlook',
'Mistral',
'Rockwell',
'Pyunji R',
'Palatino Linotype',
'LiSu',
'HoloLens MDL2 Assets',
'Bookman Old Style',
'Comic Sans MS',
'Microsoft YaHei',
'High Tower Text',
'Segoe UI',
'Franklin Gothic Medium',
'Tw Cen MT',
'Rockwell Extra Bold',
'Microsoft New Tai Lue',
'Century Schoolbook',
'Hancom MalangMalang',
'Gabriola',
'Myanmar Text',
'Lucida Sans',
'Garamond',
'Gill Sans MT Ext Condensed Bold',
'HyhwpEQ',
'Binggrae',
'Corbel',
'Matura MT Script Capitals',
'HYPost-Medium',
'Arial',
'Jokerman',
'Yu Gothic',
'Palatino Linotype',
'Engravers MT',
'Bodoni MT',
'Candara',
'Georgia',
'Broadway',
'MS Gothic',
'Modern No. 20',
'Bauhaus 93',
'HCR Dotum',
'Dubai',
'Candara',
'Book Antiqua',
'Gadugi',
'HancomEQN',
'Edwardian Script ITC',
'Hancom Gothic',
'Segoe UI Variable',
'Book Antiqua',
'Corbel',
'Arial',
'Sitka',
'Nirmala UI',
'Kristen ITC']
- 마음에 드는거 골라서 바꿔보기
In [10]:
plt.rcParams['font.family'] = 'Hancom Gothic'
히트맵 다시 출력해보기
In [11]:
sns.heatmap(gyul_growth.corr(), cmap='Greens', annot=True)
Out[11]:
<AxesSubplot: >
- 제대로 나옴!!
설정 파일 수정을 통해 default font 변경
In [12]:
import matplotlib as mpl
#font 설정 파일 위치 출력
mpl.matplotlib_fname()
Out[12]:
'C:\\Users\\Administrator\\anaconda3\\envs\\test\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc'
- 맨 마지막 matplotlibrc이 우리가 수정해야 할 파일 이름이고 경로는 바로 앞인 mpl-data까지
- 경로를 따라 들어가서 'matplotlibrc' 파일을 메모장이나 에디터로 열어주기
- ctrl+F로 font.family를 검색해서 찾으면 sans-serif로 되어있음
- 주석을 풀고 한글폰트명을 적어주기
적용 전
적용 후
- 설정 완료 후 주피터 노트북의 kernel을 리셋하고 다시 시각화 그래프를 그려도 한글 폰트가 깨지지 않음
'Python > seaborn, matplotlib' 카테고리의 다른 글
matplotlib.pylot으로 그래프 그리기 (0) | 2023.04.15 |
---|---|
plotly로 interactive chart 만들기 (0) | 2023.04.06 |
seaborn으로 기본 그래프 그리기 (0) | 2023.04.06 |
데이터 시각화/Data Visualization (0) | 2023.04.05 |