python fontproperties_如何只更改图例字体而不影响matplotlib中的其他参数?

我试图通过按照给定的步骤here定义一个全局函数来更改图例的字体。使用的代码是:import numpy as np

import matplotlib.pyplot as plt

import itertools

import matplotlib

import matplotlib.font_manager as font_manager

path = 'palatino-regular.ttf'

prop = font_manager.FontProperties(fname=path)

def change_matplotlib_font():

figures = [x for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]

for figure in figures:

for ax in figure.canvas.figure.get_axes():

ax.legend(prop = prop)

for label in ax.get_xticklabels():

label.set_fontproperties(prop)

for label in ax.get_yticklabels():

label.set_fontproperties(prop)

m = 5

n = 5

x = np.zeros(shape=(m, n))

plt.figure(figsize=(5.15, 5.15))

plt.clf()

plt.subplot(111)

marker = itertools.cycle(('o', 'v', '^', '', 's', '8', 'p'))

ax = plt.gca()

for i in range(1, n):

x = np.dot(i, [1, 1.1, 1.2, 1.3])

y = x ** 2

color = next(ax._get_lines.color_cycle)

plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next(), color=color, label = str(i))

plt.plot(x, y, linestyle='-', color = color)

plt.ylabel(r'y', labelpad=6)

plt.xlabel(r'x', labelpad=6)

# change_matplotlib_font()

plt.legend(loc = 'center left', bbox_to_anchor = (1.025, 0.5))

change_matplotlib_font()

plt.savefig('tick_font.pdf', bbox_inches='tight')

当我不调用函数change_matplotlib_font时,我得到以下输出(字体不变):

当我调用函数时,字体会改变,但位置也会改变:

在调用Python中的函数之前,如何更改保留提供的位置的字体?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值