0

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}
svenpables
  • 99
  • 1
  • 9
  • How is `SW = (-7,-1.1), NE = (8.2,1.4)` related to the current bounding box? The coordinates seem to be completely off. – samcarter_is_at_topanswers.xyz Jan 10 '22 at 09:31
  • samcarter_is_at_topanswers.xyz, I want the SW and NE coordinates of the cropped box that encompasses the graphic, and I want them to be in the same coordinate system as the curve. – svenpables Jan 10 '22 at 09:44

1 Answers1

1

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

  • Thanks samcarter_is_at_topanswers.xyz. But I want to be able to extract - ultimately print to a text file - the coordinates of the box that is cropped around the graphic. These coordinates must be in the same units and belong to the same coordinate system (number plane) as the curve. Importantly they must give the outer limits of the cropped graphic, which in this example are not the same as those for the red rectangle/ The box whose coordinates I desire is wider than the red rectangle as it encompasses the horizontal axis label, 'x'. – svenpables Jan 10 '22 at 21:14
  • @svenpables Maybe you are looking for `\pgfplotspointgetnormalizedcoordinates`, but ultimately this sounds very much like an xy-problem – samcarter_is_at_topanswers.xyz Jan 10 '22 at 21:47
  • This is closer, though it's possible something is wrong with \node (myx) at (axis cs:8.1,-0.12) {$x$};. If this is changed to \node (myx) at (axis cs:7.7,-0.12) {$x$};, the east end of the axis label is well left of the output of 8.01 (unless there's an invisible box around the label such that the x-coordinate of the east end extends beyond the actual glyph). – svenpables Jan 11 '22 at 02:03
  • @svenpables Why don't you let pgfplots do the label for you? – samcarter_is_at_topanswers.xyz Jan 11 '22 at 09:00
  • What do you mean? Stick with the default positioning? – svenpables Jan 11 '22 at 09:15
  • yes, or adapt the position to your needs. – samcarter_is_at_topanswers.xyz Jan 11 '22 at 09:20
  • Thanks again @samcarter_is_at_topanswers.xyz. You've given me plenty of good leads. Extracting the coordinates when all graphical action is kept within the pgfplots axes works for my application. On the strength of this, I'll accept your answer above. Cheers. – svenpables Jan 13 '22 at 00:21