Troubleshooting Python Hangul Breaks: Fixing Hangul Text Issues in Visualizations
In this post, we'll use the How to fix Python Hangul brokenness issuesLet's talk about the Matplotlib 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.
Previous postI'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.
import yfinance as yf
Download # S&P 500 index data
sp500 = yf.download('^GSPC', start='2010-01-01', end='2023-01-01')
Explore the # data
print(sp500.head())
print(sp500.describe())
import matplotlib.pyplot as plt
Visualize the # closing data
plt.figure(figsize=(10, 6))
plt.plot(sp500['Close'], label='S&P 500 Close', color='blue')
plt.title('S&P 500 Index Close Time Series')
plt.xlabel('Date')
plt.ylabel('Closing price (USD)')
plt.legend()
plt.show()
Why do I have Python Hangul breakage issues?
Pythonwhen visualizing in , Matplotlibuses 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.
A step-by-step guide to fixing Python Hangul brokenness
1. install Korean fonts
First, you need to install a Korean font to use in Python. On Windows, the default is Sunny Gothic It includes the same Korean fonts, but may need to be installed separately on Linux or macOS.
Linuxyou can install the Korean font using the command below:
sudo apt-get install fonts-nanumThis command uses the Shared fontsand once installed, the font will be available in Matplotlib.
2. set the Korean font in Matplotlib
Once the font is installed, we now need to enable the Korean font in Matplotlib. To do this, use the rc(runtime configuration) to specify which font to use. matplotlib.rcParamsto set the font is as follows
import yfinance as yf
Download # S&P 500 index data
sp500 = yf.download('^GSPC', start='2010-01-01', end='2023-01-01')
Explore the # data print(sp500.head())
print(sp500.describe())
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
Set # Korean font (Nanum Gothic)
font_path = 'C:/Windows/Fonts/NanumGothic.ttf' # Requires path setup
font_name = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font_name)
Visualize # Close Data
plt.figure(figsize=(10, 6))
plt.plot(sp500['Close'], label='S&P 500 Close', color='blue')
plt.title('S&P 500 Index Close Time Series')
plt.xlabel('Date')
plt.ylabel('Closing price (USD)')
plt.legend()
plt.show()This code allows you to specify a Korean font and apply it to your visualization. In the above code font_pathspecifies 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.

3. reset the font cache
Sometimes, even after setting the font, the Korean characters are still broken. In this case, use the Matplotlib Font Cachecan be helpful. You can use the command below to initialize the cache.
rm ~/.cache/matplotlib -rfIf you clear the cache and generate the graph again, the font is applied correctly.
4. redraw the graph
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.
import matplotlib.pyplot as plt
Visualize the # closing price data
plt.figure(figsize=(10, 6))
plt.plot(sp500['Close'], label='S&P 500 Close', color='blue')
plt.title('S&P 500 Index Close Time Series')
plt.xlabel('Date')
plt.ylabel('Closing price (USD)')
plt.legend()
plt.show()After running this code, the title, axis labels, and legend of your graph should now display correctly in Korean.
Cleaning up: Fixing broken Python Hangul
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? Python NAN Removal: Troubleshooting NANs in ARIMA Model Predictions Check out the post!
# Glossary
1. Matplotlib
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.
2. nan (Not a Number)
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.
3. rc (runtime configuration)
Matplotlib's rcis 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.
4. Font Cache
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.






