11

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) Commented Aug 13, 2017 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
    Commented Aug 13, 2017 at 20:06
  • @NicoSchlömer Why did you change the code? I have used matplotlib2tikz and not tikzplotlib.
    – Karlo
    Commented Jul 8, 2019 at 21:54
  • 1
    Author here. Renamed matplotlib2tikz to tikzplotlib recently. Commented Jul 9, 2019 at 6:32

2 Answers 2

8

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.

10

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .