import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns '''手动添加中文字库,甚至可以在seaborn图中显示中文。''' # # from matplotlib.font_manager import FontProperties # from matplotlib import font_manager # myfont = font_manager.FontProperties(fname=r"./SimHei.ttf", size=12) # # print('myfont:', myfont) # mpl.rcParams['font.sans-serif'] = [f'{myfont}'] # ###NOTE: 在plt中是working的,但是在sns中不working。 # # plt.hist(df['话务量'], bins=20) # plt.title(label="中文图表", fontproperties=myfont) #! working. # plt.xlabel('话务量', fontproperties=myfont) #! working. # # plt.xlabel('话务量', fontproperties=myfont) # sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark') # # sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark', font=myfont) # # sns.set(font=myfont) # sns.histplot(df['话务量'], bins=20) # plt.show() ## 查看当前字体所在的文件夹。 # import matplotlib.font_manager as fm # font_path = fm.findfont("Arial") # print(font_path) ## 将字体文件复制到matplotlib的字体文件夹中。参考: https://stackoverflow.com/questions/40290004/how-can-i-configure-matplotlib-to-be-able-to-read-fonts-from-a-local-path ##NOTE: /usr/bin/env python3 # Imports import os import re import shutil from glob import glob from matplotlib import matplotlib_fname from matplotlib import get_cachedir #! 运行前端程序前,先将中文字库装载到matplotlib中。 dir_source = './' ## 中文字体所在的文件夹。 # dir_source = '/Users/yunshi/Downloads/chatGLM/ChatGLM code_interpreter' ## 中文字体所在的文件夹。 # dir_source = '' dir_data = os.path.dirname(matplotlib_fname()) dir_dest = os.path.join(dir_data, 'fonts', 'ttf') print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.') for file in glob(os.path.join(dir_source, '*.[ot]tf')): if not os.path.exists(os.path.join(dir_dest, os.path.basename(file))): print(f'Adding font "{os.path.basename(file)}".') shutil.copy(file, dir_dest) # Delete cache dir_cache = get_cachedir() for file in glob(os.path.join(dir_cache, '*.cache')) + glob(os.path.join(dir_cache, 'font*')): if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why os.remove(file) print(f'Deleted font cache {file}.') ## 测试是否可以调用中文字体。 # df = pd.read_csv('/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/模拟数据 copy.csv') # # from matplotlib.font_manager import FontProperties # from matplotlib import font_manager # # myfont = font_manager.FontProperties(fname="./YaHei.ttf", size=12) # # print('myfont:', myfont) # mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei UI'] #! working. 注意这个名字,不是简单的YaHei。 # plt.hist(df['话务量'], bins=20) # plt.title(label="中文图表") # # plt.title(label="中文图表", fontproperties=myfont) # plt.show()