7

I have plotted some curves for different orders of a polynomial. The code and the resulting image look similar to this:

\documentclass[convert]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
 \begin{semilogxaxis}[
    width= 0.8\textwidth,
    height= 0.5\textwidth,
    ymin=0,
    ymax=40,
    xmin=0.1,
    xmax=10,        
    xlabel=frequency,
    ylabel=damping,
    grid=both,
    major grid style={black!50}
    ]
 \newcommand{\const}{0.25}
 \addplot [domain=0.01:10, samples=1000,smooth]{10*ln(1+\const*(x+1)^(2))/ln(10)};
 \addplot [domain=0.01:10, samples=1000,smooth]{10*ln(1+\const*(2*(x+1)^(2)-1)^2)/ln(10)};
 \addplot [domain=0.01:10, samples=1000,smooth]{10*ln(1+\const*(4*(x+1)^(3)-3*(x+1))^2)/ln(10)};
 \addplot [domain=0.01:10, samples=1000,smooth]{10*ln(1+\const*(8*(x+1)^(4)-8*(x+1)^2+1)^2)/ln(10)}; 
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

Output of the code above

Is there some elegant way to put labels near the curves? I imagine something like using the slopes of the different curves, and accordingly rotating the text of a node, but I don't really know how to do it. Additionaly, the labels should have white background color, but that part should be easy...

A possible outcome may be some labels like these:

Enter image description here

9

You can place labels parallel to the plot lines by adding node [pos=0.6, sloped, anchor=south] {<Text>}; at the end of the \addplot lines:

Note that I reduced the number of samples from 1000 to 30. For a function that's as smooth as this one, you don't need that many sample points.

\documentclass[convert]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
 \begin{semilogxaxis}[
    width= 0.8\textwidth,
    height= 0.5\textwidth,
    ymin=0,
    ymax=40,
    xmin=0.1,
    xmax=10,        
    xlabel=frequency,
    ylabel=damping,
    grid=both,
    major grid style={black!50},
    /tikz/plot label/.style={
        fill=white,
        sloped,
        anchor=south,
        inner sep=1pt,
        font=\bfseries
    },
    samples=30,
    domain=0.01:10
    ]
 \newcommand{\const}{0.25}
 \addplot [thick, smooth]{10*ln(1+\const*(x+1)^(2))/ln(10)} node [pos=0.65, plot label] {a};
 \addplot [thick, smooth]{10*ln(1+\const*(2*(x+1)^(2)-1)^2)/ln(10)} node [pos=0.55, plot label] {b};
 \addplot [thick, smooth]{10*ln(1+\const*(4*(x+1)^(3)-3*(x+1))^2)/ln(10)} node [pos=0.45, plot label] {c};
 \addplot [thick, smooth]{10*ln(1+\const*(8*(x+1)^(4)-8*(x+1)^2+1)^2)/ln(10)} node [pos=0.38, plot label] {d};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
  • Thanks! I somehow must have missed it in the manual... And the fill=white looks much better than anything I'd have done :) – Faekynn Jun 27 '15 at 12:43
  • I put some formulas in the node text, and now the white box sometimes inersects a bit with the curves. Some little shift of the labels would solve this... i.stack.imgur.com/ofjHd.png – Faekynn Jun 27 '15 at 12:59
  • 2
    You can set outer sep=1pt to get some separation – Jake Jun 27 '15 at 13:01

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.