googlecolab / colabtools

Python libraries for Google Colaboratory
Apache License 2.0
2.17k stars 705 forks source link

Asian fonts are not installed in Google Colab. #4113

Open 911432 opened 10 months ago

911432 commented 10 months ago

Describe the current behavior Asian region fonts are not available in Google Colab.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Generate data
data = {
    '수업회차': [1, 2, 3],
    '철수': ['8:31', '9:37', '8:53'],
    '영희': ['8:28', '8:44', '8:28'],
    '남수': ['10:54', '9:19', '10:14'],
    '희경': ['8:56', '8:59', '8:53'],
    '제희': ['9:06', '9:06', '9:09']
}
df = pd.DataFrame(data)

# Function to convert hours to minutes
def time_to_minutes(time_str):
    h, m = map(int, time_str.split(':'))
    return h * 60 + m

# Function to convert minutes to hours
def minutes_to_time(minutes):
    h = minutes // 60
    m = minutes % 60
    return f"{h}:{m:02}"

# Convert all time data to minutes
for col in df.columns:
    if col != '수업회차':
        df[col] = df[col].apply(time_to_minutes)

# Create boxplot and set Y-axis tick labels
plt.figure(figsize=(10, 6))
sns.boxplot(data=df.drop(columns=['수업회차']))
plt.yticks(ticks=range(480, 700, 30), labels=[minutes_to_time(i) for i in range(480, 700, 30)])
plt.title("출근시간")
plt.ylabel("시간")
plt.show()
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 49884 (\N{HANGUL SYLLABLE SI}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 44036 (\N{HANGUL SYLLABLE GAN}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 52636 (\N{HANGUL SYLLABLE CUL}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 44540 (\N{HANGUL SYLLABLE GEUN}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 52384 (\N{HANGUL SYLLABLE CEOL}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 49688 (\N{HANGUL SYLLABLE SU}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 50689 (\N{HANGUL SYLLABLE YEONG}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 55148 (\N{HANGUL SYLLABLE HYI}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 45224 (\N{HANGUL SYLLABLE NAM}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 44221 (\N{HANGUL SYLLABLE GYEONG}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/usr/local/lib/python3.10/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 51228 (\N{HANGUL SYLLABLE JE}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)

박스플롯

Describe the expected behavior Asian characters appear.

What web browser you are using Chrome

Additional context I wish Google Colab included Noto font.

911432 commented 10 months ago

I searched for fonts supported by Google Colab.

import matplotlib.pyplot as plt

import matplotlib.font_manager as fm
f = [f.name for f in fm.fontManager.ttflist]
print(f)
['STIXSizeFiveSym', 'DejaVu Sans Mono', 'DejaVu Serif Display', 'STIXNonUnicode', 'STIXSizeThreeSym', 'STIXNonUnicode', 'STIXGeneral', 'cmmi10', 'STIXSizeOneSym', 'STIXSizeFourSym', 'DejaVu Serif', 'DejaVu Serif', 'cmb10', 'DejaVu Sans', 'STIXNonUnicode', 'STIXGeneral', 'STIXSizeOneSym', 'STIXSizeFourSym', 'STIXSizeTwoSym', 'DejaVu Sans Mono', 'STIXGeneral', 'DejaVu Sans', 'DejaVu Sans Mono', 'cmex10', 'DejaVu Serif', 'DejaVu Sans Display', 'STIXSizeThreeSym', 'cmss10', 'DejaVu Sans', 'DejaVu Sans', 'cmsy10', 'STIXSizeTwoSym', 'DejaVu Serif', 'DejaVu Sans Mono', 'cmtt10', 'STIXNonUnicode', 'STIXGeneral', 'cmr10', 'Liberation Serif', 'Liberation Serif', 'Liberation Mono', 'Liberation Sans Narrow', 'Liberation Sans Narrow', 'Liberation Sans Narrow', 'Liberation Sans', 'Humor Sans', 'Liberation Serif', 'Liberation Sans', 'Liberation Sans', 'Liberation Sans Narrow', 'Liberation Sans', 'Liberation Mono', 'Liberation Mono', 'Liberation Mono', 'Liberation Serif']

I wish there was Noto included rather than these fonts.

cperry-goog commented 10 months ago

Thanks for the report. Tracking this internally at b/311413508

blois commented 10 months ago

Here's an example of installing fonts for use in matplotlib: https://colab.research.google.com/gist/blois/77badd478ad832250202612b6b5c5faa/matplotlib-ja.ipynb

911432 commented 9 months ago

It is inconvenient to have to install fonts for each instance.

ehsong commented 4 months ago

Here's an example of installing fonts for use in matplotlib: https://colab.research.google.com/gist/blois/77badd478ad832250202612b6b5c5faa/matplotlib-ja.ipynb

This is helpful but then again I'm not sure why Noto Sans CJK SC nor any other Noto Sans CJK KR can be displayed; Noto Sans CJK JP is only for Japanese. To elaborate I installed the NotoSansCJK.ttc.zip from the noto-website link and the file contains NotoSansCJK.ttc; however after downloading and unzipping in colab and running

import matplotlib.font_manager as fm

font_cache_file = "/root/.cache/matplotlib/fontlist-v330.json" fm_fonts = fm.json_load(font_cache_file)

font_names = [f.name for f in fm.fontManager.ttflist] for font_name in font_names: print(font_name)

Then I find that in the fontlist-v330.json there is only Noto Sans CJK JP, which does not display Chinese.

This worked for me, which is manually defining the path for the .ttf file and then specifying as fontproperties:

import matplotlib.pyplot as plt from matplotlib import font_manager

Path to your TTF font file

font_path = 'drive/MyDrive/NotoSansSC-Light.ttf'

Add the font to Matplotlib's font manager

font_manager.fontManager.addfont(font_path)

Create a font properties object

font_properties = font_manager.FontProperties(fname=font_path)

Sample Chinese text

text = "你好,世界!" # Hello, World!

Create a plot

plt.figure() plt.text(0.5, 0.5, text, fontsize=20, ha='center', fontproperties=font_properties)

Display the plot

plt.title("Test Chinese Characters in Matplotlib", fontproperties=font_properties) plt.show()