简体   繁体   English

更改 3d 表面 plot 上的标签和刻度的字体

[英]Change font for both labels and ticks on 3d surface plot

For the code below, how do I go about changing the font of all the labels and ticks to a font that is recognized by matplotlib.font_manager ?对于下面的代码,我如何 go 关于将所有标签和刻度的字体更改为matplotlib.font_manager识别的字体? I wanted to change it to URW Palladio L font, but I do not know the font family etc. as I tried this command:我想将其更改为URW Palladio L字体,但我不知道字体系列等,因为我尝试了以下命令:

plt.rcParams.update({'font.size': 18, 'font.family': 'serif', 'mathtext.fontset': 'URW Palladio L'})

but got the error:但得到了错误:

ValueError: Key mathtext.fontset: 'URW Palladio L' is not a valid value for mathtext.fontset; supported values are ['dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans', 'custom']

Here is an example code:这是一个示例代码:

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cbook
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np


# Set up plot
# Update the matplotlib configuration parameters:
#plt.rcParams.update({'font.size': 18, 'font.family': 'serif', 'mathtext.fontset': 'URW Palladio L'})
fig = plt.figure(figsize=(15, 12))
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(15, 12))

ax.set_title(f'{graph_title}')
ax.set_xlabel('Frequency (Hz)', labelpad=20)
ax.set_ylabel('Time (s)', labelpad=20)
ax.set_zlabel('PSD Amplititude (dB/Hz)', labelpad=20)

ax.view_init(20, 20)
plt.show()

Download the URW Palladio L.ttf file from here: https://www.azfonts.net/fonts/urw-palladio-l/regular-163958从此处下载 URW Palladio L.ttf 文件: https://www.azfonts.net/fonts/urw-palladio-l/regular-163958

You DON'T have to install the.ttf, just put it in an appropriate folder.您不必安装.ttf,只需将其放在适当的文件夹中即可。 Then, import matplot.font_manager , add the.tff to the font manager, then set your font.family to the name you set for that font.然后, import matplot.font_manager ,将 .tff 添加到字体管理器中,然后将font.family设置为您为该字体设置的名称。 See below:见下文:

import matplotlib.font_manager as fm

fe = fm.FontEntry(
    fname=r'YOUPATH/TO/urw-palladio-l-roman.ttf', # path to .ttf
    name='URW Palladio L')                        # what you want to name that font
fm.fontManager.ttflist.insert(0, fe)              # add the font to Matplotlib
plt.rcParams.update({'font.size': 18, 'font.family': 'URW Palladio L'}) # set the font

fig = plt.figure(figsize=(15, 12))
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(15, 12))

graph_title = 'test'
ax.set_title(f'{graph_title}')
ax.set_xlabel('Frequency (Hz)', labelpad=20)
ax.set_ylabel('Time (s)', labelpad=20)
ax.set_zlabel('PSD Amplititude (dB/Hz)', labelpad=20)

ax.view_init(20, 20)
plt.show()

Graph with the font:带字体的图表:

在此处输入图像描述

Graph with default font:使用默认字体的图表:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM