{"id":2575,"date":"2024-09-26T21:42:46","date_gmt":"2024-09-26T12:42:46","guid":{"rendered":"https:\/\/secondlife.lol\/?p=2575"},"modified":"2024-09-30T21:23:45","modified_gmt":"2024-09-30T12:23:45","slug":"fix-python-hangul-broken-issue","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/en\/fix-python-hangul-broken-issue\/","title":{"rendered":"Troubleshooting Python Hangul Breaks: Fixing Hangul Text Issues in Visualizations"},"content":{"rendered":"<p class=\"wp-block-paragraph\">In this post, we'll use the <strong>How to fix Python Hangul brokenness issues<\/strong>Let's talk about the <strong>Matplotlib<\/strong> When using the library to visualize your data, you may encounter issues with broken Korean text that doesn't display properly. Especially in titles, axis labels, and legends, broken Korean can make your graphs hard to read, so let's take a closer look at how to fix this.<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/secondlife.lol\/en\/stock-data-time-series-analysis-trend-forecasting\/\">Previous post<\/a>I'm having an issue with Korean text breaking in the graph when I run the code below, and I'll show you step by step how to fix it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\n\nDownload # S&amp;P 500 index data\nsp500 = yf.download('^GSPC', start='2010-01-01', end='2023-01-01')\n\nExplore the # data\nprint(sp500.head())\nprint(sp500.describe())\n\nimport matplotlib.pyplot as plt\n\nVisualize the # closing data\nplt.figure(figsize=(10, 6))\nplt.plot(sp500['Close'], label='S&amp;P 500 Close', color='blue')\nplt.title('S&amp;P 500 Index Close Time Series')\nplt.xlabel('Date')\nplt.ylabel('Closing price (USD)')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n<style>.kb-image2575_e95d32-f2 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2575_e95d32-f2 img.kb-img, .kb-image2575_e95d32-f2 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2575_e95d32-f2\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"376\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-24-600x376.jpg\" alt=\"\ud30c\uc774\uc36c \ud55c\uae00 \uae68\uc9d0 - S&amp;P 500 \ub370\uc774\ud130 \ub4dc\ub80c\ub4dc\" class=\"kb-img wp-image-2607\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-24-600x376.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-24-300x188.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-24-768x481.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-24.jpg 926w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Why do I have Python Hangul breakage issues?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener\">Python<\/a>when visualizing in , <strong>Matplotlib<\/strong>uses the fonts installed on your system by default. However, the default settings may not include Hangul fonts, or may not recognize Hangul properly, resulting in broken text. This can be fixed by installing Korean fonts on your system and enabling Matplotlib to recognize them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A step-by-step guide to fixing Python Hangul brokenness<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. install Korean fonts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, you need to install a Korean font to use in Python. On Windows, the default is <strong>Sunny Gothic<\/strong> It includes the same Korean fonts, but may need to be installed separately on Linux or macOS.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Linux<\/strong>you can install the Korean font using the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install fonts-nanum<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command uses the <strong>Shared fonts<\/strong>and once installed, the font will be available in Matplotlib.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. set the Korean font in Matplotlib<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once the font is installed, we now need to enable the Korean font in Matplotlib. To do this, use the <code>rc<\/code>(runtime configuration) to specify which font to use. <code>matplotlib.rcParams<\/code>to set the font is as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\n\nDownload # S&amp;P 500 index data\nsp500 = yf.download('^GSPC', start='2010-01-01', end='2023-01-01')\n\nExplore the # data print(sp500.head())\nprint(sp500.describe())\n\n<strong>import matplotlib.pyplot as plt\nfrom matplotlib import font_manager, rc\n\nSet # Korean font (Nanum Gothic)\nfont_path = 'C:\/Windows\/Fonts\/NanumGothic.ttf' # <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-theme-palette-1-color\">Requires path setup<\/mark>\nfont_name = font_manager.FontProperties(fname=font_path).get_name()\nrc('font', family=font_name)<\/strong>\n\nVisualize # Close Data\nplt.figure(figsize=(10, 6))\nplt.plot(sp500['Close'], label='S&amp;P 500 Close', color='blue')\nplt.title('S&amp;P 500 Index Close Time Series')\nplt.xlabel('Date')\nplt.ylabel('Closing price (USD)')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code allows you to specify a Korean font and apply it to your visualization. In the above code <code>font_path<\/code>specifies the path to the installed division font, and the path may vary depending on your system, so please set it accordingly. Then run the code, and you can see that the Python Hangul broken issue is completely fixed, as shown below.<\/p>\n\n\n<style>.kb-image2575_8eab3b-b1 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2575_8eab3b-b1 img.kb-img, .kb-image2575_8eab3b-b1 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2575_8eab3b-b1\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"600\" height=\"370\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-25-600x370.jpg\" alt=\"\ud30c\uc774\uc36c \ud55c\uae00 \uae68\uc9d0 - S&amp;P 500 \ub370\uc774\ud130 \ub4dc\ub80c\ub4dc\" class=\"kb-img wp-image-2609\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-25-600x370.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-25-300x185.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-25-768x473.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-25.jpg 909w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. reset the font cache<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, even after setting the font, the Korean characters are still broken. In this case, use the <strong>Matplotlib Font Cache<\/strong>can be helpful. You can use the command below to initialize the cache.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm ~\/.cache\/matplotlib -rf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you clear the cache and generate the graph again, the font is applied correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. redraw the graph<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the Korean font is set, let's try plotting the graph again. You can use the code below to verify that the Korean text is displayed correctly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\n\nVisualize the # closing price data\nplt.figure(figsize=(10, 6))\nplt.plot(sp500['Close'], label='S&amp;P 500 Close', color='blue')\nplt.title('S&amp;P 500 Index Close Time Series')\nplt.xlabel('Date')\nplt.ylabel('Closing price (USD)')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After running this code, the title, axis labels, and legend of your graph should now display correctly in Korean.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cleaning up: Fixing broken Python Hangul<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, we covered how to fix the Python Hangul brokenness issue. The key is to install a Korean font and set it up in Matplotlib. By doing this, you can create more readable and understandable graphs when Korean text is not displayed properly in your visualizations. In addition to the Python Hangul breaking problem, you'll often encounter other issues, one of which is the NAN problem, and you might be wondering what it is? <a href=\"https:\/\/secondlife.lol\/en\/fixing-nan-in-python-arima-predictions\/\" data-type=\"post\" data-id=\"2561\">Python NAN Removal: Troubleshooting NANs in ARIMA Model Predictions<\/a> Check out the post!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"># Glossary<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">1. <strong>Matplotlib<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Matplotlib is a library for data visualization in Python. It can draw many different kinds of graphs and charts, and is often used to visually represent time series data or statistical data.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2. <strong>nan (Not a Number)<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">nan stands for \"Not a Number\" and is a value displayed when a numerical calculation results in an incorrect value or missing data. It can indicate errors that occur in visualizations or data analysis.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">3. <strong>rc (runtime configuration)<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Matplotlib's <code>rc<\/code>is short for \"runtime configuration\" and is a configuration file that allows you to change global settings for your graph. You can adjust the overall style of the graph, including fonts, colors, line styles, and more.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">4. <strong>Font Cache<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">The font cache is a temporary storage area where font information is stored for quick access. Graphing with an incorrect font cache can cause fonts to not be applied correctly, so it's useful to delete and recreate the cache when there are problems.<\/p>","protected":false},"excerpt":{"rendered":"<p>\uc774\ubc88 \ud3ec\uc2a4\ud2b8\uc5d0\uc11c\ub294 \ud30c\uc774\uc36c \ud55c\uae00 \uae68\uc9d0 \ubb38\uc81c\ub97c \ud574\uacb0\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574 \uc124\uba85\ud558\uaca0\uc2b5\ub2c8\ub2e4&#8230;.<\/p>","protected":false},"author":3,"featured_media":2628,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3],"tags":[162,259,172,33,258,257],"class_list":["post-2575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-coding","tag-matplotlib","tag-259","tag-172","tag-33","tag-258","tag-257"],"taxonomy_info":{"category":[{"value":3,"label":"\ud30c\uc774\uc36c(Python)"}],"post_tag":[{"value":162,"label":"Matplotlib"},{"value":259,"label":"\ub098\ub214 \uace0\ub515"},{"value":172,"label":"\uc2dc\uac01\ud654"},{"value":33,"label":"\ud30c\uc774\uc36c"},{"value":258,"label":"\ud3f0\ud2b8 \uc124\uc815"},{"value":257,"label":"\ud55c\uae00 \uae68\uc9d0"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/\ud30c\uc774\uc36c-\ub370\uc774\ud130\ubd84\uc11d-\uc608\uc81c-\ud3ec\uc2a4\ud2b8-\uc378\ub124\uc77c-1-600x600.jpg",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/en\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":3,"name":"\ud30c\uc774\uc36c(Python)","slug":"python-coding","term_group":0,"term_taxonomy_id":3,"taxonomy":"category","description":"","parent":20,"count":119,"filter":"raw","cat_ID":3,"category_count":119,"category_description":"","cat_name":"\ud30c\uc774\uc36c(Python)","category_nicename":"python-coding","category_parent":20}],"tag_info":[{"term_id":162,"name":"Matplotlib","slug":"matplotlib","term_group":0,"term_taxonomy_id":162,"taxonomy":"post_tag","description":"","parent":0,"count":19,"filter":"raw"},{"term_id":259,"name":"\ub098\ub214 \uace0\ub515","slug":"%eb%82%98%eb%88%94-%ea%b3%a0%eb%94%95","term_group":0,"term_taxonomy_id":259,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":172,"name":"\uc2dc\uac01\ud654","slug":"%ec%8b%9c%ea%b0%81%ed%99%94","term_group":0,"term_taxonomy_id":172,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"},{"term_id":33,"name":"\ud30c\uc774\uc36c","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac","term_group":0,"term_taxonomy_id":33,"taxonomy":"post_tag","description":"","parent":0,"count":30,"filter":"raw"},{"term_id":258,"name":"\ud3f0\ud2b8 \uc124\uc815","slug":"%ed%8f%b0%ed%8a%b8-%ec%84%a4%ec%a0%95","term_group":0,"term_taxonomy_id":258,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":257,"name":"\ud55c\uae00 \uae68\uc9d0","slug":"%ed%95%9c%ea%b8%80-%ea%b9%a8%ec%a7%90","term_group":0,"term_taxonomy_id":257,"taxonomy":"post_tag","description":"","parent":0,"count":2,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/comments?post=2575"}],"version-history":[{"count":10,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"predecessor-version":[{"id":2755,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2575\/revisions\/2755"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media\/2628"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}