|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 futui 于 2023-4-26 11:40 编辑
我用matplotlib,乱码,要在这找个中文字体
麻烦大师看看,下面有没有几个是中文字体的,谢谢
[FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\cmmi10.ttf', name='cmmi10', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSansMono.ttf', name='DejaVu Sans Mono', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSansMono-Bold.ttf', name='DejaVu Sans Mono', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\cmb10.ttf', name='cmb10', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizOneSymBol.ttf', name='STIXSizeOneSym', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSerif-Italic.ttf', name='DejaVu Serif', style='italic', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSerif.ttf', name='DejaVu Serif', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXNonUniBolIta.ttf', name='STIXNonUnicode', style='italic', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizThreeSymBol.ttf', name='STIXSizeThreeSym', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXGeneral.ttf', name='STIXGeneral', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXNonUni.ttf', name='STIXNonUnicode', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXGeneralBolIta.ttf', name='STIXGeneral', style='italic', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizThreeSymReg.ttf', name='STIXSizeThreeSym', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizOneSymReg.ttf', name='STIXSizeOneSym', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizTwoSymBol.ttf', name='STIXSizeTwoSym', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXNonUniBol.ttf', name='STIXNonUnicode', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\cmtt10.ttf', name='cmtt10', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSansDisplay.ttf', name='DejaVu Sans Display', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXGeneralItalic.ttf', name='STIXGeneral', style='italic', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\cmss10.ttf', name='cmss10', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSansMono-Oblique.ttf', name='DejaVu Sans Mono', style='oblique', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizTwoSymReg.ttf', name='STIXSizeTwoSym', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSerifDisplay.ttf', name='DejaVu Serif Display', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\DejaVuSans-Bold.ttf', name='DejaVu Sans', style='normal', variant='normal', weight=700, stretch='normal', size='scalable'),
FontEntry(fname='c:\\Users\\cenghua\\anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\STIXSizFourSymReg.ttf', name='STIXSizeFourSym', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
...
FontEntry(fname='C:\\Windows\\Fonts\\LTYPEB.TTF', name='Lucida Sans Typewriter', style='normal', variant='normal', weight=600, stretch='normal', size='scalable'),
FontEntry(fname='C:\\Windows\\Fonts\\javatext.ttf', name='Javanese Text', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='C:\\Windows\\Fonts\\REFSAN.TTF', name='MS Reference Sans Serif', style='normal', variant='normal', weight=400, stretch='normal', size='scalable'),
FontEntry(fname='C:\\Windows\\Fonts\\PERTILI.TTF', name='Perpetua Titling MT', style='normal', variant='normal', weight=300, stretch='normal', size='scalable'),
FontEntry(fname='C:\\Windows\\Fonts\\CALIST.TTF', name='Calisto MT', style='normal', variant='normal', weight=400, stretch='normal', size='scalable')]
从您提供的字体列表中,我没有发现任何中文字体。要解决matplotlib中的中文乱码问题,您可以尝试安装一个中文字体,并在您的代码中使用它。
首先,从网上下载一个中文字体(例如,微软雅黑、宋体、黑体等),并将其安装到您的系统字体文件夹中。然后,您可以在matplotlib的rcParams设置中更改默认字体,或在绘制文本时指定所需的中文字体。以下是一个使用微软雅黑字体的示例:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 指定微软雅黑字体
my_font = FontProperties(fname="C:\\Windows\\Fonts\\msyh.ttc")
plt.figure()
plt.plot([0, 1], [0, 1])
# 使用指定的字体
plt.xlabel("横轴", fontproperties=my_font)
plt.ylabel("纵轴", fontproperties=my_font)
plt.title("中文标题", fontproperties=my_font)
plt.show()
如果您不想在每次绘制文本时都指定字体,可以将微软雅黑字体设置为matplotlib的默认字体:
import matplotlib.pyplot as plt
import matplotlib
# 设置默认字体为微软雅黑
matplotlib.rcParams['font.family'] = 'Microsoft YaHei'
matplotlib.rcParams['font.serif'] = 'Microsoft YaHei'
matplotlib.rcParams['font.sans-serif'] = 'Microsoft YaHei'
plt.figure()
plt.plot([0, 1], [0, 1])
# 使用默认字体
plt.xlabel("横轴")
plt.ylabel("纵轴")
plt.title("中文标题")
plt.show()
如果您的系统中没有微软雅黑字体,可以将其替换为其他可用的中文字体。只需将字体文件路径更改为相应的路径即可。
|
|