3

I'd like to use a pgfplots axis environment to render some plots. I'd like them to be aligned with the surrounding TiKz graphics. However, I have not yet managed to find the correct options. I'm also only interested in the plotted line itself, so no ticks or axes need to be drawn in addition. So far I have this (failed) example. My goal would be aligning the coordinate system the axis environment such that the two curves are right on top of each other. With the width/height I assumed I could set the exact dimensions, but that already seems to be off.

Is there a way to modify this axis-environment to match the coordinate system of the tikzpicture-environment?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\draw[lightgray] (-2, -3) grid (4, 4);
\fill[lightgray] (0,0) circle (0.2);  % origin
\draw[green] plot [red, domain=-2:4, samples=200] (\x, {exp(-abs(\x)^2)});
\begin{axis}[
    xtick=\empty,
    ytick=\empty,
    hide axis,
    xmin=-2, xmax=4,
    axis equal,
    width=6cm,
    height=7cm,
    anchor=south west,
    at={(-2cm,-2cm)},
]
\addplot [red, domain=-2:5, samples=200] (\x, {exp(-abs(\x)^2)});
\end{axis}
\end{tikzpicture}
\end{document}

the goal is adjusting the axis environment such that the two curves match

1 Answer 1

3

Use explicit limits for both axes and make the physical size of the axis match the corresponding coordinate ranges. Also add scale only axis, otherwise width and height include extra space reserved by pgfplots.

In this example the TikZ grid goes from x=-2 to x=4 and from y=-3 to y=4, so use:

\begin{axis}[
    hide axis,
    scale only axis,
    xmin=-2, xmax=4,
    ymin=-3, ymax=4,
    width=6cm,
    height=7cm,
    anchor=south west,
    at={(-2cm,-3cm)},
]

The important corrections are:

ymin=-3, ymax=4

because otherwise pgfplots chooses its own vertical scale, and

scale only axis

because otherwise width and height do not refer only to the actual plotting area:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}

\draw[lightgray] (-2, -3) grid (4, 4);
\fill[lightgray] (0,0) circle (0.2);  % origin

\draw[green, domain=-2:4, samples=200,line width=4pt]
  plot (\x, {exp(-abs(\x)^2)});

\begin{axis}[
    hide axis,
    scale only axis,
    xmin=-2, xmax=4,
    ymin=-3, ymax=4,
    width=6cm,
    height=7cm,
    anchor=south west,
    at={(-2cm,-3cm)},
]
\addplot [red, domain=-2:4, samples=200]
  ({x}, {exp(-abs(x)^2)});
\end{axis}

\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.