Issue
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
? I wanted to change it to URW Palladio L
font, but I do not know the font family etc. as I tried this command:
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()
Solution
Download the URW Palladio L .ttf file from here: 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. 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. 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:
Answered By - Michael S.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.