Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The python plotting library matplotlib will by default add margins to any plot that it generates. They can be reduced to a certain degree through some options of savefig(), namely bbox_inches='tight' and pad_inches=0. Even with those options, some margins will always remain, though.

If a plot generated by matplotlib is to be used in conjunction with pgfplots and the addplot graphics feature, those margins need to be predictable (and ideally non-existent).

Q: How can these margins be got rid of completely?

share|improve this question

1 Answer

up vote 3 down vote accepted

The following example takes its key idea from http://stackoverflow.com/a/4328608/179927.

import numpy as np
import matplotlib.pyplot as pl

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig = pl.figure(figsize=(8,5))
ax = pl.gca()
pl.axis('off')
pl.plot(x, y)
pl.xlim(0,10)
pl.ylim(-1,1)

extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
pl.savefig('out.pdf', bbox_inches=extent)

plot without margins

It generates a sine plot completely without margins. The resulting pdf can then be integrated into a pgfplots plot like this:

\documentclass{scrartcl}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis on top,
    plot graphics/xmin=0,
    plot graphics/xmax=10,
    plot graphics/ymin=-1,
    plot graphics/ymax=1,
    enlargelimits=false]
    \addplot graphics {out};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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