8

I have a tikzpicture generated in Python using tikzplotlib:

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib

vector = np.array([1, 2, 3])
plt.figure(figsize=(6, 4))
plt.plot(vector, vector)
tikzplotlib.save('test.tex')

Now if I change the figsize in Python, the image size in my Python environment will indeed change. But in a LaTeX document, where I import the tikzpicture using \input{test.tex}, the size is not changed. How can I define the size in either Python or LaTeX, without stretching the text/numbers as well, that should remain the standard size of the LaTeX text? The solutions mentioned in this question do not solve my problem.

4
  • 1
    The answer to that is actually given in the Readme of matplotlib2tikz: github.com/nschloe/matplotlib2tikz (just scroll down a bit) – Torbjørn T. Aug 13 '17 at 20:00
  • Indeed. I had not searched in the right place. Point 2 of Usage answers the question. I invite you to repeat your comment as an answer, and I will accept it. – Karlo Aug 13 '17 at 20:06
  • @NicoSchlömer Why did you change the code? I have used matplotlib2tikz and not tikzplotlib. – Karlo Jul 8 '19 at 21:54
  • 1
    Author here. Renamed matplotlib2tikz to tikzplotlib recently. – Nico Schlömer Jul 9 '19 at 6:32
7

As described in the Readme, tikzplotlib has the possibility of setting the width and/or height of the plots, using the figurewidth/figureheight keys for tikz_save:

tikz_save('someplot.tex', figureheight='5cm', figurewidth='9cm')

I believe that these lengths refer to the size of the axis box alone, not including ticklabels, axis labels and titles.

You can alternatively set the values to macros, and define lengths by those names in your document, i.e.

tikz_save(
    'someplot.tex',
    figureheight = '\\figH',
    figurewidth = '\\figW'
    )

and in your LaTeX document:

\documentclass{article}
\usepackage{pgfplots,amsmath}

\newlength\figH
\newlength\figW
\setlength{\figH}{4cm}
\setlength{\figW}{8cm}

\begin{document}

Some text, then a centred plot:

\begin{center}
\input{firstplot}
\end{center}

More text, then a wider plot:

\begin{center}
\setlength{\figW}{10cm} % when added inside the  center environment it has no effect outside it
\input{secondplot}
\end{center}

\end{document}

This is basically the same as for matlab2tikz (See my answer to tikz+matlab2tikz), except that the key is figurewidth/figureheight, instead of width/height.

4

In version 0.9.3 you seem to need axis_height and axis_width, rather than figureheight and figurewidth.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.