1

I am generating pgfplot files (PGF) with Python and matplotlib.pyplot as well as the following configuration for pgf export to be used with pdfLatex compiler:

import matplotlib as mpl
mpl.use("pgf")
pgf_with_pdflatex = {
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         #r"\usepackage{cmbright}",
         ]
}

Using the comon plt.plot() commands and so on into Python, I manage to generate a 20cm wide plot and then export it into a pgf file using the plt.savefig('Graphes/pompage_pCav.pgf', format = 'pgf') Pyplot command.

That pgf file seems pretty proper. I then include it into a tex file as a figure:

\begin{figure}
    \begin{center}
        \input{Figures/pompage_pCav.pgf}
    \end{center}
    \caption{test}
    \label{test}
\end{figure}

The page size is A4 (so 21cm wide) and here is the result: It is not centered :(

As you can see, the figure isn't centered whether I use the center latex environment or the \centering command. How to solve this issue? Thanks.

  • as you have not provided a usable example it is hard to be sure, but it looks centred to me from your image – David Carlisle Apr 25 '18 at 14:23
  • 1
    If your figure is wider that \textwidth, that will cause a shift in the fashion of your image. If this is the case, there are several options, the simplest being to shrink your figure down to a width of \textwidth. – Steven B. Segletes Apr 25 '18 at 14:24
  • 1
    note that the labels on the left are part of the image so the caption is not centred under the rectangular frame but under the complete image including the labels – David Carlisle Apr 25 '18 at 14:27
  • I suggest to use \centering instead of the center environment, to avoid additional vertical spaces. – samcarter_is_at_topanswers.xyz Apr 25 '18 at 15:03

2 Answers2

2

If you generate a 20cm wide plot for a 21cm page, that's likely to cause problems, as it leaves 1 cm for right and left margin together. if the left margin is larger than .5cm, your figure will extend into the right margin. The only solution for that would be to generate a plot that's smaller than the distance between left and right margin (aka \textwidth).

remco
  • 1,017
  • Alright ! Thank you for your answer. The thing is I didn't know how to print out \textwidth. Indeed my image was too large. Thanks to all. – Odyseus_v4 Apr 25 '18 at 14:39
0

Thanks to all for your quick answers.

Indeed my image was too large. I displayed the textwidth with \printinunitsof{in}\prntlen{\textwidth} and its value is 6.69423in. Then changed the figure width to 6.69in with a 1.3 (~4/3) aspect ratio. Compiling the Tex file outputs 9 errors saying the Dimension is too large. I don't understand. Although, as I'm using ShareLaTeX, the pdf compiles and the plot is therefore properly centered. How to solve those issues though.

 Figures/pompage_pCav.pgf, line 1772
Dimension too large.

<to be read again> 
                   \relax 
l.1772 ...ansformshift{-1416.023250in}{4.191678in}
                                                  %
I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

! Dimension too large.
<to be read again> 
                   \relax 
l.1877 ...ansformshift{-1416.023250in}{4.191678in}
                                                  %
I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

9 errors display the same text.

By the way, is it possible to change the pgf image width (and hight?) as it is with a pdf or png? Looks like this format isn't quite versatile unlike tickz (i couldn't get to install the matplotlib2tikz package using Anaconda on Windows).

For those asking about usable example: Python:

import numpy as np
import matplotlib.pyplot as plt 
plt.figure(figsize = (6.69, 5.017500000000001))

# do some plotting here
x = np.linspace(-2, 2, 100)
plt.plot(x, x)

# save to file
plt.savefig('test.pgf')

And in latex:

\documentclass[11pt, a4paper]{report}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern}
\usepackage{xstring}
\usepackage{layouts}
\usepackage{pgfplots}
\pgfplotsset{height = 7cm,width=12cm,compat=1.9}

\begin{document}
\begin{figure}
    \begin{center}
        \input{test.pgf}
    \end{center}
    \caption{textwidth = \printinunitsof{in}\prntlen{\textwidth}}
    \label{test}
\end{figure}
\end{document}

Link to the pgf file I use: test.pgf (save in .pgf file)