6

I admit that I may be abusing pgfplots somewhat, but I want to add a plot of a function into a tikz-picture in a certain way. I have now managed to get this to run, I'm just wondering why the line thickness varies, and how to get rid of it, resp. what part of my code is responsible for it (however I can say that it's not the scaling nor the rotation).

I've mostly drawn upon Consistently specify a Function and use it for computation and plotting and How to define a parameterized command to be consumable in \pgfmathdeclarefunction?, and managed to extend it by nesting one further layer of functions with \edef.

As a side note, I'm amazed what \pgfmathparse can deal with (because after expanding all the functions, it's a long expression, and longer still in my actual implementation)

Below is the (cut-to-basics) MWE:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}

\newcommand{\T}[1]{3*(#1)^2-2*(#1)^3}
\edef\U#1{sin((\T{#1})*pi/2 r)}

\pgfmathdeclarefunction{q}{1}{%
    \pgfmathparse{(and(#1>1, #1<2)*\U{#1-1})+(and(#1>=2, #1<4)*\U{2-#1/2})}%
}

\begin{document}
\begin{tikzpicture}[scale=1.5]
    \begin{scope} %[very thin]
        \draw[->] (0,0) -- (5,0);
        \draw[->] (0,0) -- (0,4);
    \end{scope}

    \draw (1,1) -- (4,4);
    \coordinate (x) at (1,1.25);

    \pgfmathsqrt{2}
    \pgfmathsetmacro{\mysc}{\pgfmathresult}
    \begin{scope}[rotate=45,scale=\mysc]
    \begin{axis}[
        at=(x.north), % doesn't work with explicit coordinate {(1,1.25)}
        domain=1:4, samples=200,
        hide axis,
        ymin=0, ymax=1,
        xmin=1, xmax=4,
        width=3cm, height=0.5cm, scale only axis,
        ]
        \addplot [cyan] {q(x)};
    \end{axis}
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

7

I think what is going on is that hide axis is somehow still drawing the axis lines (but in white) and these are fading out portions your graph. If you comment out the hide axis in your MWE you will see this.

According the pgfplots manual:

hide axis=true|false: Allows to hide either a selected axis or all of them. No outer rectangle, no tick marks and no labels will be drawn. Only titles and legends will be processed as usual. Axis scaling and clipping will be done as if you did not use hide axis.

with the last line being the relevant line. So if you simply add clip=false the problem goes away:

enter image description here

Code:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}

\newcommand{\T}[1]{3*(#1)^2-2*(#1)^3}
\edef\U#1{sin((\T{#1})*pi/2 r)}

\pgfmathdeclarefunction{q}{1}{%
    \pgfmathparse{(and(#1>1, #1<2)*\U{#1-1})+(and(#1>=2, #1<4)*\U{2-#1/2})}%
}

\begin{document}
\begin{tikzpicture}[scale=1.5]
    \begin{scope} %[very thin]
        \draw[->] (0,0) -- (5,0);
        \draw[->] (0,0) -- (0,4);
    \end{scope}

    \draw (1,1) -- (4,4);
    \coordinate (x) at (1,1.25);

    \pgfmathsqrt{2}
    \pgfmathsetmacro{\mysc}{\pgfmathresult}
    \begin{scope}[rotate=45,scale=\mysc]
    \begin{axis}[
        at=(x.north), % doesn't work with explicit coordinate {(1,1.25)}
        domain=1:4, samples=200,
        hide axis, 
        clip=false,% <-- Adding this fixes it
        ymin=0, ymax=1,
        xmin=1, xmax=4,
        width=3cm, height=0.5cm, scale only axis,
        ]
        \addplot [cyan] {q(x)};
    \end{axis}
    \end{scope}
\end{tikzpicture}
\end{document}
  • Ah, that makes perfect sense - the outer rectangle was obscuring about half the line width... Thanks a lot! Ps. Any idea why at=... disregards an explicit coorinate? – Axel Oct 7 '14 at 23:27
  • @Axel: I suggest you post a separate question as it is unrelated to the unintended line thickness which was the focus of this question. – Peter Grill Oct 8 '14 at 5:20
  • Well, it didn't seem important enough to warrant its own question... – Axel Oct 8 '14 at 8:42

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.