0

I want to draw energy cascade graph same as below:

enter image description here

I just assumed that the first part is a constant function equals 4 and the second and the third parts are linear functions with different slopes.

I used the following codes:

\begin{tikzpicture}[
declare function={
    func(\x)= (\x>=1) * (4)+
    and(\x>1.5, \x<=4) * (-\x/2)+
    and(\x>4, \x<=5) * (-\x);
}
]
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=0, ymax=5, ytick={0,...,5}, ylabel=$\log{E}$,
xmin=0, xmax=5, xtick={-5,...,5}, xlabel=$k$,
]
\pgfplotsinvokeforeach{1, 3, 4}{
    \draw[dashed] ({rel axis cs: 0,0} -| {axis cs: #1, 0}) -- ({rel axis cs: 0,20} -| {axis cs: #1, 0});}
\addplot[blue]{func(x)};
\end{axis}
\end{tikzpicture}

But the output gives a continuous curve and also I can't replace the xticks numbers with mathematical phrases.

enter image description here

How can I graph a plot same as the first picture?

1

The problem with your plot is in the function you declared. You have the desired slope but not the y-intercept point. Also you need many samples for the change in the slope to be smooth. I'd rather split the function in three plots or change it for some coordinates.

For the ticks labels you need the xticklabels command.

Below is the code with two plots: yours modified and another without the function.

\documentclass[border=2mm]{standalone}
\usepackage {pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
% OP code modified
\begin{tikzpicture}[
declare function={
    func(\x) = (\x<=1) * (4) +
               and(\x>1, \x<=4) * (-\x/2+4.5) +
               (\x>4) * (-\x+6.5);
}
]
\begin{axis}
[
axis x line=middle, axis y line=middle, samples=101, domain=0.5:4.5,
ymin=0, ymax=5, ytick={0,...,5}, ylabel=$\log{E}$,
xmin=0, xmax=5, xtick={1,3,4}, xlabel=$k$,
xticklabels={$1/L$, $1/l$, $1/l_k$}, yticklabels={}
]
\pgfplotsinvokeforeach{1, 3, 4}
{
    \addplot[red,dashed] coordinates {(#1,0) (#1,5)};
} 
\addplot[blue]{func(x)};
\end{axis}
\end{tikzpicture}

% Suggested code
\begin{tikzpicture}
\begin{axis}
[
axis x line=middle, axis y line=middle,
ymin=0, ymax=5, ytick={0,...,5}, ylabel=$\log{E}$,
xmin=0, xmax=5, xtick={1,3,4}, xlabel=$k$,
xticklabels={$1/L$, $1/l$, $1/l_k$}, yticklabels={}
]
\pgfplotsinvokeforeach{1, 3, 4}
{
    \addplot[red,dashed] coordinates {(#1,0) (#1,5)};
} 
\addplot[blue] coordinates {(0.5,4) (1,4) (4,2.5) (4.5,2)};
\end{axis}
\end{tikzpicture}
\end{document}

And the plots: enter image description here EDIT: Removed the unnecessary y-axis tick labels.

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.