Note on custom matplotlib style


This serves as a note for set­ting up my cus­tom mat­plotlib style. This style fea­tures,

  1. Combines the styles of seaborn-deep, seaborn-ticks (comes with seaborn li­brary), and science
  2. Render text and math us­ing LaTeX
  3. Use font CMU Bright. This font is used for ren­der­ing both text and equa­tions.

The con­tent of the mat­plotlib style sheet file is the fol­low­ing,

# Use LaTeX for math formatting
text.usetex : True

# In general, we need to be careful with the preamble.
# Use CMU bright font
text.latex.preamble : \usepackage{amsmath} \usepackage{amssymb} \usepackage{cmbright} \usepackage[OT1]{fontenc}

# Seaborn common parameters
figure.facecolor: white
text.color: .15
axes.labelcolor: .15
legend.frameon: False
legend.numpoints: 1
legend.scatterpoints: 1
xtick.color: .15
ytick.color: .15
axes.axisbelow: True
image.cmap: Greys
font.family: sans-serif
grid.linestyle: -
lines.solid_capstyle: round

# Seaborn white parameters
axes.grid: False
axes.facecolor: white
axes.edgecolor: .15
axes.linewidth: 1.25
grid.color: .8

# Seaborn deep palette
axes.prop_cycle: cycler('color', ['4C72B0', '55A868', 'C44E52', '8172B2', 'CCB974', '64B5CD'])
patch.facecolor: 4C72B0

# Set default figure size
figure.figsize : 3.75, 2.5

# Set x axis
xtick.direction : in
xtick.major.size : 3
xtick.major.width : 0.5
xtick.minor.size : 1.5
xtick.minor.width : 0.5
xtick.minor.visible : True

# Set y axis
ytick.direction : in
ytick.major.size : 3
ytick.major.width : 0.5
ytick.minor.size : 1.5
ytick.minor.width : 0.5
ytick.minor.visible : True

lines.markersize : 5.5

# Always save as 'tight'
savefig.bbox : tight
savefig.pad_inches : 0.05

However, there is a catch. When us­ing LaTeX to ren­der the text and math, if the file is saved as pdf for­mat, the font is al­ways em­bed­ded as Type 1 font. This cause miss­ing font is­sue if opened by Adobe Illustrator or Inkscape, and the text won’t be prop­erly dis­played. To re­solve this, I found two meth­ods:

  1. Save as .svg for­mat. The out­put file can be prop­erly opened by Adobe Illustrator and Inkscape
  2. Save as .pdf for­mat, and then use Ghostscript to con­vert all text to out­line. The com­mand is the fol­low­ing,
gs -o new.pdf -dNoOutputFonts -sDEVICE=pdfwrite old.pdf

The is­sue with both of these meth­ods is that you can not edit the text be­cause the text is out­lined. So far, I am not aware of any method that can do both La­TeX ren­der­ing and does­n’t have miss­ing font is­sue.

Below are two ex­am­ples of this cus­tom style (both are slighted mod­i­fied from the ex­am­ples shown on the mat­plotlib doc­u­ment). If they are too small, you can right click on them and open them in a new tab.

Example 1
Example 2: More equa­tions ren­dered us­ing LaTeX

The script for gen­er­at­ing these plots can be found here