pythonmatplotlibtex

How to obtain the same font(-style, -size etc.) in matplotlib output as in latex output?


I have one .tex-document in which one graph is made by the python module matplotlib. What I want is, that the graph blends in to the document as good as possible. So I want the characters used in the graph to look exactly like the other same characters in the rest of the document.

My first try looks like this (the matplotlibrc-file):

text.usetex   : True
text.latex.preamble: \usepackage{lmodern} #Used in .tex-document
font.size    : 11.0 #Same as in .tex-document
backend: PDF

For compiling of the .tex in which the PDF output of matplotlib is included, pdflatex is used.

Now, the output looks not bad, but it looks somewhat different, the characters in the graph seem weaker in stroke width.

What is the best approach for this?

EDIT: Minimum example: LaTeX-Input:

\documentclass[11pt]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\includegraphics{./graph}
\caption{Excitation-Energy}
\label{fig:graph}
\end{figure}

\end{document}

Python-Script:

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1,2,3,4])
plt.xlabel("Excitation-Energy")
plt.ylabel("Intensität")
plt.savefig("graph.pdf")

PDF output:

enter image description here


Solution

  • The difference in the fonts can be caused by incorrect parameter setting out pictures with matplotlib or wrong its integration into the final document. I think problem in text.latex.preamble: \usepackage{lmodern}. This thing works very badly and even developers do not guarantee its workability, how you can find here. In my case it did not work at all.

    Minimal differences in font associated with font family. For fix this u need: 'font.family' : 'lmodern' in rc. Other options and more detailed settings can be found here.

    To suppress this problem, I used a slightly different method - direct. plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]. It is not strange, but it worked. Further information can be found at the link above.


    To prevent these effects suggest taking a look at this code:

    import matplotlib.pyplot as plt
    
    #Direct input 
    plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
    #Options
    params = {'text.usetex' : True,
              'font.size' : 11,
              'font.family' : 'lmodern',
              'text.latex.unicode': True,
              }
    plt.rcParams.update(params) 
    
    fig = plt.figure()
    
    #You must select the correct size of the plot in advance
    fig.set_size_inches(3.54,3.54) 
    
    plt.plot([1,2,3,4])
    plt.xlabel("Excitation-Energy")
    plt.ylabel("Intensität")
    plt.savefig("graph.pdf", 
                #This is simple recomendation for publication plots
                dpi=1000, 
                # Plot will be occupy a maximum of available space
                bbox_inches='tight', 
                )
    

    And finally move on to the latex:

    \documentclass[11pt]{scrartcl}
    
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern}
    \usepackage{graphicx}
    
    \begin{document}
    
    \begin{figure}
        \begin{center}
            \includegraphics{./graph}
            \caption{Excitation-Energy}
            \label{fig:graph}
        \end{center}
    \end{figure}
    
    \end{document}
    

    Results

    Zoom of pdf document

    As can be seen from a comparison of two fonts - differences do not exist (1 - MatPlotlib, 2 - pdfLaTeX) Comparison of fonts