2017-07-07 3 views
0

Ich möchte eine Matplotlib-Figur mit einer x-Achsenbeschriftung erstellen, die in Arial-Schriftart ist und ein Wort kursiv geschrieben hat.Matplotlib xlabel in Arial mit einem Wort kursiv geschrieben

Ich kann Figuren mit x-Achsen-Etiketten in Latex-Schriftart mit einem Wort kursiv erstellen; Ich kann auch Zahlen mit x-Achsen-Beschriftungen in Arial erstellen, solange entweder die gesamte Beschriftung kursiv oder die gesamte Beschriftung normal ist. aber ich kann keinen Teil des xlabel in Arial kursiv und den anderen Teil in arial normal bekommen.

Ich bin meistens Permutationen des folgenden Code versuchen:

from matplotlib import pyplot as plt 
import numpy as n 
import matplotlib 
from matplotlib.font_manager import FontProperties 
font = {'family' : 'Arial', 
    'weight' : 'medium', 
    'size' : 20, 
    'style' : 'normal'} 
font0 = {'family' : 'Arial', 
    'weight' : 'medium', 
    'size' : 20, 
    'style' : 'italic'} 


matplotlib.rcParams['mathtext.fontset'] = 'custom' 
matplotlib.rcParams['mathtext.rm'] = 'Arial' 
matplotlib.rcParams['mathtext.it'] = 'Arial' 


matplotlib.rc('font', **font) 
#matplotlib.rc('font', **font0) 
matplotlib.rc('text', usetex=False) 

plt.figure(); plt.plot(n.linspace(0,3,10), n.linspace(0,3,10)) 
plt.xlabel(r'$\mathit{italics}$ - rest normal') 

Antwort

1
from matplotlib.pyplot import * 

# Need to use precise font names in rcParams; I found my fonts with 
#>>> import matplotlib.font_manager 
#>>> [f.name for f in matplotlib.font_manager.fontManager.ttflist] 

rcParams['mathtext.fontset'] = 'custom' 
rcParams['mathtext.it'] = 'Arial:italic' 
rcParams['mathtext.rm'] = 'Arial' 

fig, ay = subplots() 


# Using the specialized math font elsewhere, plus a different font 
xlabel(r"$\mathit{Italic}$ $\mathrm{and\ just\ Arial}$ and not-math-font",fontsize=18) 
# No math formatting, for comparison 
ylabel(r'Italic and just Arial and not-math-font', fontsize=18) 
grid() 

show() 

enter image description here

+0

Fantastic !! Das funktioniert, und die Idee mit dem Font Manager ist großartig - danke! – mzzx

Verwandte Themen