Alex
Alex

Reputation: 73

Make a label both Italic and Bold style in Matplotlib with stix font

I am trying to generate a plot with matplotlib and I use the 'stix' font (rcParams['mathtext.fontset'] = 'stix') in order to have smooth font size transitions from text to math text. However, some of my math symbols I want to be Italic (scalar values) and some to be Italic AND Bold (tensors). I do not want to go through the solution of using Latex rendering cause then other things are messed up.

I will give you a small example that depicts the problem:

from numpy import *
from matplotlib.pyplot import * 

# Chaning font to stix
rcParams['mathtext.fontset'] = 'stix'

# Some data to constract this plotting example
datax=[0,1,2]
datay=[8,9,10]
datay2=[8,15,10]

fig, ay = subplots()

ay.plot(datax, datay, color="0.", ls='-', label= r"$F_{\alpha}$")
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$")

# Now add the legend with some customizations.
legend = ay.legend(loc='left', shadow=True)

#frame = legend.get_frame()
#frame.set_facecolor('0.90')
xlabel(r"x label",fontsize=18)
ylabel(r'y label', fontsize=18)
grid()

show()

If you run the code the first label is Italic and the second label is Bold. How could I achieve the second label to be Bold AND Italic?

Problem with math text to be Italic and bold

Upvotes: 6

Views: 12250

Answers (3)

Zaus
Zaus

Reputation: 1351

As of matplotlib version 3.8 there is the \mathbfit command:

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Nimbus Roman'
plt.rcParams['mathtext.fontset'] = 'stix'

fig, ax = plt.subplots()
ax.plot([], label= r"italic (default): $F_\alpha$")
ax.plot([], label=r"roman: $\mathrm{F_\alpha}$")
ax.plot([], label=r"bold: $\mathbf{F_\alpha}$")
ax.plot([], label=r"bold and italic: $\mathbfit{F_\alpha}$")
ax.legend(fontsize='xx-large')

bold and italic math text

Upvotes: 0

cphlewis
cphlewis

Reputation: 16249

A few more specific mathtext params are needed:

from numpy import *
from matplotlib.pyplot import *

# Changing font to stix; setting specialized math font properties as directly as possible
rcParams['mathtext.fontset'] = 'custom'
rcParams['mathtext.it'] = 'STIXGeneral:italic'
rcParams['mathtext.bf'] = 'STIXGeneral:italic:bold'

# Some data to construct this plotting example
datax=[0,1,2]
datay=[8,9,10]
datay2=[8,15,10]

fig, ay = subplots()

ay.plot(datax, datay, color="0.", ls='-', label= r"$\mathit{F_{\alpha}}$")
ay.plot(datax, datay2, color="0.", ls='-', label=r"$\mathbf{F_{\alpha}}$")

# Now add the legend with some customizations.
legend = ay.legend(loc='left', shadow=True)

# Using the specialized math font again
xlabel(r"$\mathbf{x}$ label",fontsize=18)
ylabel(r'y label', fontsize=18)
grid()

show()

enter image description here

Note that I used the mathbf in an axis label, too. Would probably change the rest of the label to a STIX font, which you can define a non-italic-non-bold case of: see the docs under 'Custom fonts'.

There are also at least two examples in the matplotlib Gallery that might help: one looking at font families and another reminding us which STIX fonts are which.

Upvotes: 9

fjafjan
fjafjan

Reputation: 84

So MatplotLib is using LaTeX syntax, so my best guess is that you can use LaTeX syntax for how to get italicized and bold, which is

 \textbf{\textit{text}}

So in your case it would be

ay.plot(datax, datay2, color="0.", ls='-', label= r"$\mathbf{ \textit{F_{\alpha}} }$")

Upvotes: -3

Related Questions