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
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:
Without the box:





\addplotmacro should be put inside theaxisenvironment. If this does not help, please post a full and compilable code example. – Jasper Habicht Nov 18 '22 at 00:44My boxes around the content are causing the error. Do you know why?
– LostInHilbertSpace Nov 18 '22 at 01:59tikzpictures. Your workaround is a way to go, that is, saving the contents as a box first. You could also try using atcolorboxfor the box. – Jasper Habicht Nov 18 '22 at 06:55tikzpictures... – Rmano Nov 18 '22 at 15:44