Upload 2 files
Browse files- add_fonts.py +72 -0
- requirements.txt +0 -1
add_fonts.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
# from ydata_profiling import ProfileReport
|
4 |
+
# from streamlit_pandas_profiling import st_profile_report
|
5 |
+
import matplotlib as mpl
|
6 |
+
import seaborn as sns
|
7 |
+
|
8 |
+
'''手动添加中文字库,甚至可以在seaborn图中显示中文。'''
|
9 |
+
# # from matplotlib.font_manager import FontProperties
|
10 |
+
# from matplotlib import font_manager
|
11 |
+
# myfont = font_manager.FontProperties(fname=r"./SimHei.ttf", size=12)
|
12 |
+
# # print('myfont:', myfont)
|
13 |
+
# mpl.rcParams['font.sans-serif'] = [f'{myfont}']
|
14 |
+
|
15 |
+
# ###NOTE: 在plt中是working的,但是在sns中不working。
|
16 |
+
# # plt.hist(df['话务量'], bins=20)
|
17 |
+
# plt.title(label="中文图表", fontproperties=myfont) #! working.
|
18 |
+
# plt.xlabel('话务量', fontproperties=myfont) #! working.
|
19 |
+
# # plt.xlabel('话务量', fontproperties=myfont)
|
20 |
+
|
21 |
+
# sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark')
|
22 |
+
# # sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark', font=myfont)
|
23 |
+
# # sns.set(font=myfont)
|
24 |
+
# sns.histplot(df['话务量'], bins=20)
|
25 |
+
# plt.show()
|
26 |
+
|
27 |
+
|
28 |
+
## 查看当前字体所在的文件夹。
|
29 |
+
# import matplotlib.font_manager as fm
|
30 |
+
# font_path = fm.findfont("Arial")
|
31 |
+
# print(font_path)
|
32 |
+
|
33 |
+
|
34 |
+
## 将字体文件复制到matplotlib的字体文件夹中。参考: https://stackoverflow.com/questions/40290004/how-can-i-configure-matplotlib-to-be-able-to-read-fonts-from-a-local-path
|
35 |
+
##NOTE: /usr/bin/env python3
|
36 |
+
# Imports
|
37 |
+
import os
|
38 |
+
import re
|
39 |
+
import shutil
|
40 |
+
from glob import glob
|
41 |
+
from matplotlib import matplotlib_fname
|
42 |
+
from matplotlib import get_cachedir
|
43 |
+
|
44 |
+
#! 运行前端程序前,先将中文字库装载到matplotlib中。
|
45 |
+
dir_source = '/Users/yunshi/Downloads/chatGLM/ChatGLM code_interpreter' ## 中文字体所在的文件夹。
|
46 |
+
# dir_source = '<your-font-directory-here>'
|
47 |
+
dir_data = os.path.dirname(matplotlib_fname())
|
48 |
+
dir_dest = os.path.join(dir_data, 'fonts', 'ttf')
|
49 |
+
print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.')
|
50 |
+
for file in glob(os.path.join(dir_source, '*.[ot]tf')):
|
51 |
+
if not os.path.exists(os.path.join(dir_dest, os.path.basename(file))):
|
52 |
+
print(f'Adding font "{os.path.basename(file)}".')
|
53 |
+
shutil.copy(file, dir_dest)
|
54 |
+
|
55 |
+
# Delete cache
|
56 |
+
dir_cache = get_cachedir()
|
57 |
+
for file in glob(os.path.join(dir_cache, '*.cache')) + glob(os.path.join(dir_cache, 'font*')):
|
58 |
+
if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why
|
59 |
+
os.remove(file)
|
60 |
+
print(f'Deleted font cache {file}.')
|
61 |
+
|
62 |
+
## 测试是否可以调用中文字体。
|
63 |
+
# df = pd.read_csv('/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/模拟数据 copy.csv')
|
64 |
+
# # from matplotlib.font_manager import FontProperties
|
65 |
+
# from matplotlib import font_manager
|
66 |
+
# # myfont = font_manager.FontProperties(fname="./YaHei.ttf", size=12)
|
67 |
+
# # print('myfont:', myfont)
|
68 |
+
# mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei UI'] #! working. 注意这个名字,不是简单的YaHei。
|
69 |
+
# plt.hist(df['话务量'], bins=20)
|
70 |
+
# plt.title(label="中文图表")
|
71 |
+
# # plt.title(label="中文图表", fontproperties=myfont)
|
72 |
+
# plt.show()
|
requirements.txt
CHANGED
@@ -22,4 +22,3 @@ streamlit_authenticator==0.2.3
|
|
22 |
streamlit_pandas_profiling==0.1.3
|
23 |
tqdm==4.66.2
|
24 |
xlrd==2.0.1
|
25 |
-
ydata_profiling==4.6.4
|
|
|
22 |
streamlit_pandas_profiling==0.1.3
|
23 |
tqdm==4.66.2
|
24 |
xlrd==2.0.1
|
|