Issue
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
Solution
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()
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.
Answered By - cphlewis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.