I can produce plots on matplotlib and export them to pgf successfully. But I believe that by first defining a base font size on matplotlib rcParams (11pt for example), and then defining things like axes tick labels to be \small, they become fixed at 10 pt when I insert them to LaTeX. I believe this because when I change the class font size, the plot text does not change.

Now let's say I use the same plot in a different document, poster, or beamer presentation. I would like the axes tick label to be \small (relative to base font), and title to be \normalsize for example.

I believe the answer lies in the mpl.rcParams.update() method.

Here is some of Python code, and below, the .tex code. When I change the class font size, ideally the plot font would scale with it.

import matplotlib as mpl
import matplotlib.pyplot as plt

import numpy as np

    "text.usetex": True,
    "pgf.texsystem": "xelatex",
    "pgf.rcfonts": False,
    "font.family": "sans-serif",
    'figure.titlesize': 'large',
    'ytick.labelsize':   'small',
    "font.serif": [],
    "font.sans-serif": [],
    "font.monospace": [],
    "figure.figsize": [3,4],
    "pgf.preamble": [
# mpl.rc('text.latex', preamble=r'\usepackage{kpfonts}')

t = np.linspace(0,5)
x = np.exp(-t)*np.sin(2*t)

plt.title('TESTING title size')
plt.ylabel('TESTING Y label size')
plt.xlabel('TESTING X label size')


-- LaTeX code below

123456 Example plot below (normal sized text) {\small (Small Text)} {\footnotesize Smaller! Text}\\

Any insight is appreciated!

  • Please provide some example code (both for matplotlib and for LaTeX) that reproduces the answer, i.e., a short example document with a graph in it where the font sizes are incorrect. This allows potential answerers to focus on the issue instead of spending time trying to recreate your situation. – Marijn Sep 13 at 19:56
  • I'll try to make a MWE as soon as possible... I thought that this might be a common question with an easy fix. – likethevegetable Sep 13 at 21:06
  • Edited it to include sample code. – likethevegetable Sep 13 at 21:25
  • I'm not sure this is possible, as matplotlib produces code such as \fontsize{10}{12} \selectfont foo, i.e. the fontsize is set explicitly. I don't really think the output is intended for flexibility at all. Have you tried matplotlib2tikz? – Torbjørn T. Sep 13 at 22:23
  • @likethevegetable Thanks for the code, the question has improved a lot. – Marijn Sep 14 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.