Python Matplotlib:使用带有本地路径的字体时,图例fontsize不会更改

Python Matplotlib:使用带有本地路径的字体时,图例fontsize不会更改,python,matplotlib,Python,Matplotlib,我使用python3.7,matplotlib来可视化图形上的多条线。我的最终目标是只缩小图例字体大小,但是我希望使用与代码所在文件夹相同的字体 当我使用“mpl.rcParams['font.family']=font.get_name()”字体时,图例的字体确实变小了。但是,我不希望代码依赖于特定计算机字体中的文件,我希望它能够使用特定字体,无论代码是否在我的计算机上,这就是我使用本地路径的原因 所有东西都显示正确,除了大小没有改变,无论我怎么尝试 import pandas as pd f

我使用python3.7,matplotlib来可视化图形上的多条线。我的最终目标是只缩小图例字体大小,但是我希望使用与代码所在文件夹相同的字体

当我使用“mpl.rcParams['font.family']=font.get_name()”字体时,图例的字体确实变小了。但是,我不希望代码依赖于特定计算机字体中的文件,我希望它能够使用特定字体,无论代码是否在我的计算机上,这就是我使用本地路径的原因

所有东西都显示正确,除了大小没有改变,无论我怎么尝试

import pandas as pd
from datetime import datetime, timedelta
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates
from matplotlib.cm import get_cmap
import matplotlib.font_manager as font_manager
from cycler import cycler
from matplotlib.pyplot import figure
from collections import OrderedDict
from collections import Counter
from collections import defaultdict
from itertools import accumulate
import numpy as np


path = 'fonts/GT-America-Extended-Medium.OTF'
font = font_manager.FontProperties(fname=path)
pathofregularfont = "fonts/GT-America-Expanded-Regular.OTF"
fontregular = font_manager.FontProperties(fname=pathofregularfont)

plt.figure(figsize=(16,9))

cmap = get_cmap(name)   
colors = cmap.colors  
plt.rc('axes', prop_cycle=(cycler('color', colors)))

for index, user in zip(range(int(limit)), counted.keys()):
        print(index, user)
        listofdatesforeveryuser = list(reversed(dictofusersanddates.get(user)))
        counteddates = OrderedDict(Counter(listofdatesforeveryuser))
        cumulativecounts = list(accumulate(list(counteddates.values())))
        dateslist = [datetime.strptime(date, '%Y-%m-%d').date() for date in counteddates.keys()]
        print(user, dateslist)



        if int(limit) <= 17:

            plt.legend(prop=font)

        elif int(limit) > 17:

            plt.legend(prop=fontregular)
            mpl.rcParams["legend.fontsize"] = 6

        plt.step(dateslist, cumulativecounts, linestyle='solid', linewidth=0.5, label=(user + '\n' + str(counted.get(user)) + ' likes')) 

ax = plt.axes()
for label in ax.get_xticklabels():
    label.set_fontproperties(fontregular)
    #ax.set_fontproperties(font)
for label in ax.get_yticklabels():
    label.set_fontproperties(fontregular)
name = str(filename).replace('.csv','')
plt.savefig('schedules/Top ' + str(limit) + ' users for ' + name + '.png',dpi=600)
将熊猫作为pd导入
从datetime导入datetime,timedelta
将matplotlib导入为mpl
从matplotlib导入pyplot作为plt
从matplotlib导入日期为mpl_日期
从matplotlib.cm导入获取cmap
将matplotlib.font\u管理器导入为font\u管理器
从cycler导入cycler
从matplotlib.pyplot导入图
从集合导入订单
从收款进口柜台
从集合导入defaultdict
从itertools导入
将numpy作为np导入
路径='fonts/GT America Extended Medium.OTF'
font=font\u管理器.FontProperties(fname=path)
pathofregularfont=“font/GT America Expanded Regular.OTF”
fontregular=font_manager.FontProperties(fname=PathFregularFont)
plt.图(figsize=(16,9))
cmap=get\u cmap(名称)
颜色=cmap.colors
plt.rc('axes',prop_cycle=(cycler('color',colors)))
对于索引,zip(范围(int(limit)),counted.keys()中的用户:
打印(索引,用户)
listofdatesforeveryuser=列表(反转(dictOfUsandDates.get(用户)))
counteddates=OrderedDict(计数器(listofdatesforeveryuser))
cumulativecounts=list(累计(list(counteddates.values()))
dateslist=[datetime.strTime(日期,%Y-%m-%d')。counteddates.keys()中日期的日期()
打印(用户、日期列表)
如果int(限制)17:
plt.图例(道具=fontregular)
mpl.rcParams[“legend.fontsize”]=6
plt.step(dateslist,cumulativecounts,linestyle='solid',linewidth=0.5,label=(user+'\n'+str(counted.get(user))+'likes'))
ax=plt.axs()
对于ax.Getxticklabels()中的标签:
label.set_fontproperties(fontregular)
#ax.set_fontproperties(字体)
对于ax.get_yticklabels()中的标签:
label.set_fontproperties(fontregular)
name=str(文件名).replace('.csv','')
plt.savefig('schedules/Top'+str(limit)+'user for'+name+'.png',dpi=600)
我尝试过(定位在plt.legend之前和之后,以及plt.step和plt.savefig之前):
plt.legend(fontsize=6)

我尝试过(定位在plt.legend前后以及plt.step和plt.savefig之前):
mpl.rcParams[“legend.fontsize”]=6
plt.rcParams[“legend.fontsize”]=6

我试过(放在plt.legend之前,plt.step和plt.savefig之前):
plt.rc('legend',fontsize='small')

我尝试过(定位在plt.legend之后,plt.step和plt.savefig之前):
plt.legend(fontsize=6)

我试过(定位在plt.legend之后,plt.step和plt.savefig之前):
plt.rc('legend',fontsize=6)

我还在plt.step之后和plt.savefig之前尝试了这些选项


字体仍然保持不变,在某些情况下,如果我将fontsize=6语句放在plt.legend(prop=font)之后,它将选择matplotlib默认字体和较小字体,并将忽略我指定的字体。

在创建图例之前,您肯定需要设置fontsize。谢谢,我们将对此进行更多测试。我尝试了:plt.legend(fontsize=6)和plt.rcParams[“legend.fontsize”]=6和plt.rc('legend',fontsize=6)和mpl.rcParams[“legend.fontsize”]=6-在创建图例之前,但仍然不起作用。我会继续努力。