Search code examples
latexcoordinatesboundarytikzpgfplots

Bounding box coordinates for pgfplot


How can I get the bounding box coordinates for the pgfplot below? I need the coordiates in pgfplot space. (current axis.south west) and (current bounding box.south west), for example, do not give what I need. I'm expecting something like SW = (-7,-1.1), NE = (8.2,1.4), which would extend to the right beyond the red rectangle.

\documentclass[border=0pt,multi=my]{standalone}
\usepackage{amssymb,amsmath}
\usepackage[pass,paperheight=\maxdimen]{geometry}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{xcolor}
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{calc,shapes,positioning,fit,decorations,decorations.text,decorations.pathmorphing,backgrounds,patterns,arrows,arrows.meta}
\usepackage{ifthen}
\usepackage{pagecolor}

\tikzset{every picture/.style={line width=0.02cm}}

\begin{document}

\pagestyle{empty}

\begin{my}
\begin{tikzpicture}[tight background,background rectangle/.style={draw=none,fill=red!45},show background rectangle,scale=0.85]
    \begin{axis}[
      name=foo,
      width=360pt,
        height=210pt,
        xmin=-7,
        xmax=8,
        ymin=-1.1,
        ymax=1.4,
        xtick={0},
        xticklabels={$0$},
        ytick={0},
        yticklabels={$0$},
        extra x ticks={-6.283185307,-4.71238898,-3.141592654,-1.570796327,0,1.570796327,3.141592654,4.71238898,6.283185307},
    extra x tick labels={$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
        extra x tick style={
            xticklabel style={below right},
        },
        extra y ticks={-1,1},
    extra y tick labels={$-1$,$1$},
        extra y tick style={
            yticklabel style={right,xshift=5pt},
        },          
        axis x line=center,
        axis y line=center]
        \addplot[line width=0.0325cm, cyan!60!black, domain=-7:7, samples=200] {sin(deg(x))};
        \node at (axis cs:8.1,-0.12) {$x$};
        \node[anchor=west] at (axis cs:0.1,1.3) {$y$};
    \end{axis}
\end{tikzpicture}
\end{my}

\end{document}

Solution

  • It sounds as if you are looking for \pgfplotspointgetnormalizedcoordinates{}

    \documentclass[border=0pt,multi=my]{standalone}
    \usepackage{amssymb,amsmath}
    \usepackage[pass,paperheight=\maxdimen]{geometry}
    \usepackage{graphicx}
    \usepackage{epsfig}
    \usepackage{xcolor}
    \usepackage{pgf,tikz,pgfplots}
    \usetikzlibrary{calc,shapes,positioning,fit,decorations,decorations.text,decorations.pathmorphing,backgrounds,patterns,arrows,arrows.meta}
    \usepackage{ifthen}
    \usepackage{pagecolor}
    
    \tikzset{every picture/.style={line width=0.02cm}}
    
    \begin{document}
    
    \pagestyle{empty}
    
    \begin{my}%
    \begin{tikzpicture}[tight background,background rectangle/.style={draw=none,fill=red!45},show background rectangle,scale=0.85]
        \begin{axis}[
          name=foo,
          width=360pt,
            height=210pt,
            xmin=-7,
            xmax=8,
            ymin=-1.1,
            ymax=1.4,
            xtick={0},
            xticklabels={$0$},
            ytick={0},
            yticklabels={$0$},
            extra x ticks={-6.283185307,-4.71238898,-3.141592654,-1.570796327,0,1.570796327,3.141592654,4.71238898,6.283185307},
        extra x tick labels={$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
            extra x tick style={
                xticklabel style={below right},
            },
            extra y ticks={-1,1},
        extra y tick labels={$-1$,$1$},
            extra y tick style={
                yticklabel style={right,xshift=5pt},
            },          
            axis x line=center,
            axis y line=center]
            \addplot[line width=0.0325cm, cyan!60!black, domain=-7:7, samples=200] {sin(deg(x))};
            \node (myx) at (axis cs:8.1,-0.12) {$x$};
            \node[anchor=west] at (axis cs:0.1,1.3) {$y$};
    
            \node[green] at (200,10) {
             \pgfplotspointgetnormalizedcoordinates{(xticklabel cs:0)} 
             \pgfmathprintnumber{\pgfkeysvalueof{/data point/x}}
             \pgfplotspointgetnormalizedcoordinates{(myx.east)} 
             \pgfmathprintnumber{\pgfkeysvalueof{/data point/x}}
             \pgfplotspointgetnormalizedcoordinates{(yticklabel cs:0)} 
             \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
             \pgfplotspointgetnormalizedcoordinates{(yticklabel cs:1)} 
             \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}         
            };
            \draw[green, ultra thick] (axis cs:-7,-1.1) rectangle (axis cs:8.41,1.4);
        \end{axis}
    \end{tikzpicture}%
    \end{my}%
    
    \end{document}
    

    enter image description here