20

I want to put a pgfplots plot beside several other pdf images that I import into my document, but I can't figure out how to position the pgfplots axis in the larger tikzpicture:

\documentclass{article}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{figure*}

  \begin{center}
    \begin{tikzpicture}

       \node[anchor=south west] (img) at (0,0) 
            {\includegraphics[width=0.3\linewidth]{1.pdf}};
       \node[anchor=south west] 
            at (0.01\linewidth,0.01\linewidth) {a};        

       \node[anchor=south west] (img) at (0.33\linewidth,0) 
            {\includegraphics[width=0.3\linewidth]{1.pdf}};
       \node[anchor=south west] 
            at (0.34\linewidth,0.01\linewidth) {b}; 

       \begin{axis}[width=0.45\linewidth, yticklabels={}]
         \addplot [const plot, fill=red] 
                  table [x index=0, y index=1]
                  {hist.txt}
         \closedcycle;  
       \end{axis}

    \end{tikzpicture}
  \end{center}

  \caption{\label{detdemo}An example of my output}
\end{figure*}
\end{document}

here is the example output: example output

The axis seems to begin from the (0,0) of the tikzpicture.

I wanted to see how I can position this axis after the second picture? Such that its bottom left corner is on (0.66\linewidth,0).

2
  • You want the plot to come after the second picture?
    – user11232
    Mar 24, 2014 at 10:44
  • Yes, I want the bottom left point of the plot to be on (0.66\linewidth,0).
    – makhlaghi
    Mar 24, 2014 at 10:46

2 Answers 2

20

You can define the coordinate for the lower left corner of the axis with the at key,

at={(0.66\linewidth,0)}

Add this to the axis options.

enter image description here

\documentclass{article}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{figure*}

  \begin{center}
    \begin{tikzpicture}

       \node[anchor=south west] (img) at (0,0) 
            {\includegraphics[width=0.3\linewidth]{example-image-a}};
       \node[anchor=south west] 
            at (0.01\linewidth,0.01\linewidth) {a};        

       \node[anchor=south west] (img) at (0.33\linewidth,0) 
            {\includegraphics[width=0.3\linewidth]{example-image-b}};
       \node[anchor=south west] 
            at (0.34\linewidth,0.01\linewidth) {b}; 

       \begin{axis}[width=0.45\linewidth, yticklabels={},at={(0.66\linewidth,0)}]
         \addplot [const plot, fill=red] 
                  {x}
         \closedcycle;  
       \end{axis}

    \end{tikzpicture}
  \end{center}

  \caption{\label{detdemo}An example of my output}
\end{figure*}
\end{document}
3
  • Is there a way to set the position of the axis using its center (instead of the lower left corner)?
    – pelegs
    Aug 17, 2021 at 12:09
  • 1
    @pelegs axis-environments also have anchors. Can't remember all the names, have to look in the manual later, but anchor=center might work. Aug 17, 2021 at 12:12
  • Together with "at={(current page.center)}" it worked (in beamer). Thanks!
    – pelegs
    Aug 17, 2021 at 16:54
8

As a quick hack, you can either use a scope with a proper xshift or put the axis inside another node.

\documentclass{article}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{positioning}

\begin{document}
\begin{figure*}

  \begin{center}
    \begin{tikzpicture}

       \node[anchor=south west] (img1) at (0,0)
            {\includegraphics[width=0.3\linewidth]{example-image-a}};
       \node[anchor=south west]
            at (0.01\linewidth,0.01\linewidth) {a};

       \node[anchor=south west] (img2) at (0.33\linewidth,0)
            {\includegraphics[width=0.3\linewidth]{example-image-b}};
       \node[anchor=south west]
            at (0.34\linewidth,0.01\linewidth) {b};
       \begin{scope}[xshift=0.66\linewidth] 
       \begin{axis}[width=0.45\linewidth, yticklabels={}]
         \addplot [const plot, fill=red]
                  {x}       %% I changed this change it back
         \closedcycle;
       \end{axis}
       \end{scope}

    \end{tikzpicture}
  \end{center}

  \caption{\label{detdemo}An example of my output comes here}
\end{figure*}
\end{document}

enter image description here

But it will be better if you use another tikzpicture environment IMO.

3
  • See my answer ... Mar 24, 2014 at 11:08
  • 1
    @TorbjørnT. Technically both do the same :) Yours is more pgfplots way though.
    – user11232
    Mar 24, 2014 at 11:15
  • Saved me, the answer by TorbjørnT did not work for some reason.
    – Karlo
    Dec 21, 2020 at 23:45

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.