1

I am plotting a simple straight line graph. Actually, I'm plotting a modulus, or absolute, function which I had no success with and using two linear plots to do it instead.

My code is the following:

\pgfkeys{/pgfplots/axis labels at tip/.style={
 xlabel style={at={(current axis.right of origin)}, yshift=1.5ex, anchor=north},
 ylabel style={at={(current axis.above origin)}, xshift=1.5ex, anchor=east}}
 }

\begin{minipage}[t]{0.42\columnwidth} \begin{tikzpicture} \begin{axis}[ xmin=-2, xmax=3, ymin=-4, ymax=6, scale only axis=true, scale=0.4, domain=-2:3, axis lines=middle, samples=500, xtick=\empty, ytick=\empty, ylabel={$y$}, xlabel={$x$}, axis labels at tip, ] \end{axis} \addplot [maincolor,restrict y to domain={0:6} ] { 2*x - 2}; \end{tikzpicture}

\end{minipage}

And this is the erroneous result

Restricting domain of linear graph so that graph is cut off at x-axis gives error

What I've tried to do is cut the graph off at the x-axis. But this graph is cut off below the x-axis. Why is this?

EDIT:

Here is a compilable example with some of the environments I have the graph in:

\documentclass{article}

\usepackage{multicol} \usepackage{tikz} \usepackage{pgfplots} \usepackage{ninecolors} \usepackage{environ}

\colorlet{maincolor}{violet5}

% Define boxes \tikzstyle{mybox} = [draw=maincolor, fill=white, very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt] \tikzstyle{fancytitle} =[fill=maincolor, text=white, rounded corners, font=\bfseries]

\NewEnviron{chbox}[1] { \begin{tikzpicture} \node [mybox] (box){% \minipage{0.3\textwidth} \fontsize{7pt}{8pt}\selectfont \BODY \par \endminipage }; \node[fancytitle, right=10pt] at (box.north west) { #1 }; \end{tikzpicture} }

\pgfkeys{/pgfplots/axis labels at tip/.style={ xlabel style={at={(current axis.right of origin)}, yshift=1.5ex, anchor=north}, ylabel style={at={(current axis.above origin)}, xshift=1.5ex, anchor=east}} }

\begin{document}

\begin{multicols*}{3}

   \begin{chbox}{Example}
       \begin{minipage}[t]{0.42\columnwidth}
           \begin{tikzpicture}
              \begin{axis}[
                 xmin=-2, xmax=3,
                 ymin=-4, ymax=6,
                 scale only axis=true,
                 scale=0.4,
                 domain=-2:3,
                 axis lines=middle,
                 samples=500,
                 xtick=\empty,
                 ytick=\empty,
                 ylabel={$y$},
                 xlabel={$x$},
                 axis labels at tip,
               ]
               \addplot [maincolor,restrict y to domain={0:6} ] { 2*x - 2};
              \end{axis}
             \end{tikzpicture}
            \end{minipage}
           \end{chbox}  
          \end{multicols*}

        \end{document}

It turns out that it seems to be my box around the plot which causes the error. If I remove the box, it cuts off perfectly at the x-axis.

Problem is, I need the boxes.

With the box:

With box

Without the box:

enter image description here

  • The \addplot macro should be put inside the axis environment. If this does not help, please post a full and compilable code example. – Jasper Habicht Nov 18 '22 at 00:44
  • @JasperHabicht Hi, please see my edits. Changing addplot inside the axis environment did not work, I actually moved it out to see if it changed anything.

    My boxes around the content are causing the error. Do you know why?

    – LostInHilbertSpace Nov 18 '22 at 01:59
  • It has to be something to do with the fact I'm placing a pgfplot as a node. I've removed everything else from the chbox definition and it still gives the error. What is it about using a pgfplot inside a node that makes it distort the graph? How else should I go about drawing the boxes? – LostInHilbertSpace Nov 18 '22 at 03:30
  • 1
    You need to take care when nesting tikzpictures. Your workaround is a way to go, that is, saving the contents as a box first. You could also try using a tcolorbox for the box. – Jasper Habicht Nov 18 '22 at 06:55
  • https://tex.stackexchange.com/questions/47377/proper-nesting-of-tikzpicture-environments-reset-all-pgf-values-to-their-defaul : thou shall not nest tikzpictures... – Rmano Nov 18 '22 at 15:44

2 Answers2

1

You can use the fit library to avoid nesting of tikzpictures which can easily cause alignment problems:

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{fit} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \usepackage{ninecolors} \usepackage{environ}

\colorlet{maincolor}{violet5}

\tikzset{ mybox/.style={ draw=maincolor, very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=10pt }, fancytitle/.style={ fill=maincolor, text=white, rounded corners, font=\bfseries }, /pgfplots/axis labels at tip/.append style={ xlabel style={ at={(current axis.right of origin)}, yshift=-1.5ex, anchor=north }, ylabel style={ at={(current axis.above origin)}, xshift=-1.5ex, anchor=east } } }

\NewEnviron{chbox}[1]{ \begin{tikzpicture}[font={\fontsize{7pt}{8pt}\selectfont}] \BODY \node[mybox, fit={(current bounding box.north east) (current bounding box.south west)}] (box) {}; \node[fancytitle, right=10pt] at (box.north west) {#1}; \end{tikzpicture} }

\begin{document}

\begin{chbox}{Example}
    \begin{axis}[
        xmin=-2, xmax=3,
        ymin=-4, ymax=6,
        scale only axis=true,
        scale=0.4,
        domain=-2:3,
        axis lines=middle,
        samples=500,
        xtick=\empty,
        ytick=\empty,
        ylabel={$y$},
        xlabel={$x$},
        axis labels at tip,
    ]
    \addplot[maincolor, restrict y to domain={0:6}] {2*x - 2};
    \end{axis}
\end{chbox}  

\end{document}

enter image description here

0

I found a workaround to this.

The problem seems to be from the pgfplot being embedded in two tikzpictures, one for the box and one for the actual plot. Perhaps something happens to the coordinates when this happens.

So I used savebox which typesets it once before the tikzpicture and then just dumps it in.

I would still appreciate if somebody could tell me why the coordinates become confused and if there is a more natural way of doing this within tikz and pgfplots, rather than using saveboxes.

My workaround:

\newsavebox\mybox

\begin{lrbox}{\mybox} \begin{tikzpicture} \begin{axis}[ xmin=-2, xmax=3, ymin=-4, ymax=6, scale only axis=true, scale=0.4, domain=-2:3, axis lines=middle, samples=500, xtick=\empty, ytick=\empty, ylabel={$y$}, xlabel={$x$}, axis labels at tip, ] \addplot [maincolor,restrict y to domain={0:6} ] { 2*x - 2}; \end{axis} \end{tikzpicture} \end{lrbox}

\begin{chbox}{Example} \begin{minipage}[t]{0.42\columnwidth} \usebox\mybox \end{minipage} \end{chbox}

Fixed

Now I'll just readjust the axis labels.