5

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

Jongware
  • 22,200
  • 8
  • 54
  • 100
Alex
  • 63
  • 1
  • 1
  • 8
  • I don't think it's possible. You may need to use true latex (i.e. usetex=True) and then this question might help: https://tex.stackexchange.com/questions/14395/bold-italic-vectors – ImportanceOfBeingErnest Jun 27 '17 at 18:44

2 Answers2

9

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.

cphlewis
  • 15,759
  • 4
  • 46
  • 55
  • 1
    This is so great! 1000 upvotes. I've read the documentation on the `mathtext` rcParams, but couldn't figure out the syntax for specifying the particular stix font varants. Thanks so much. – SpinUp __ A Davis Dec 04 '21 at 19:30
  • 1
    :) I couldn't get there from the docs either, I think I had to read some source. However there are now two official examples and a new rcParam -- added at the end of the answer. – cphlewis Jan 14 '22 at 17:54
-3

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}} }$")
fjafjan
  • 84
  • 1
  • 6