3

Using the following code, I draw the lines manually after trials and errors to locate the coordinates of the tick marks. Is there a way to take the tick marks positions as references to draw from.

    \documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
 tick style={line width=.04cm, color=black, line cap=round},
 font=\normalsize,color=black,
 xmin=0,xmax=64,
 xtick={0,16,32},xticklabels={0,16,32},
 ymin=0,ymax=16,
 ytick={4,8},yticklabels={4,8},
 tickwidth=.2cm,
 xlabel={P}, xlabel style={right},
 ylabel={M}, ylabel style={above},
 xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
 yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
 samples=200]
\end{axis}
\draw [ultra thick,blue] (0,2.84) -- +(-40:4.44cm) node [pos=.5,circle, draw, black, fill=black, scale=0.5]{} node [pos=0,circle, draw, black, fill=black, scale=0.5]{}  node [pos=1,circle, draw, black, fill=black, scale=0.5]{};
\draw [thick, black, densely dotted] (1.7128,1.424) node [black, xshift=.46cm, yshift=.05cm] {A} -- (0,1.424);
\draw [thick, black, densely dotted] (1.7128,1.424) -- (1.7128,0);
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

There are other points which locations are related to the tick marks locations; as in the following drawing

enter image description here

Hany
  • 4,709

2 Answers2

3

If you draw inside the axis environment, you can use the axis cs: for that. To show that it works for arbitrary functions, I've added a 'complex' function. Note that I added the clip=false option to the axis to prevent the marks from being clipped.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}

\tikzset{
    declare function={
%        myslope2(\x) = 24 - \x*(24/48);
        myslope(\x) = 8 - \x*(8/32);
        mycomplexfunction(\x) = sin(60*pow(\x,1.2)) + 6;
    },
}

\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
 tick style={line width=.04cm, color=black, line cap=round},
 font=\normalsize,color=black,
 xmin=0,xmax=64,
 xtick={0,16,32},xticklabels={0,16,32},
 ymin=0,ymax=16,
 ytick={4,8},yticklabels={4,8},
 tickwidth=.2cm,
 xlabel={P}, xlabel style={right},
 ylabel={M}, ylabel style={above},
 xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
 yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
 samples=200,
 clip=false]
    \draw[ultra thick,blue] plot[samples at={0,16,32},mark=*,mark options={black}] (axis cs:\x,{myslope(\x)});
    \draw[thick,black,densely dotted] (axis cs:0,{myslope(16)}) -- (axis cs:16,{myslope(16)}) node [black, xshift=.46cm, yshift=.05cm] {A} -- (axis cs:16,0);
    \draw[thin,red] plot[domain=0.1:32,smooth,samples=200] (axis cs:\x,{mycomplexfunction(\x)});
    \draw[thin,red,densely dotted] (axis cs:0,{mycomplexfunction(20)}) -- (axis cs:20,{mycomplexfunction(20)}) -- (axis cs:20,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

It looks like this:

enter image description here

For the example image you showed, you need the function myslope2(\x) = 24 - \x*(24/48);. Just add the samples in the same way as above.

Edit
As pointed out in the comments by @TorbjørnT, using \pgfplotset{compat=1.11} or higher lets the axis cs be the default coordinate system inside the axis from \draw commands. Also if you use \addplot instead of \draw plot, the markers are not clipped when they protrude outside the axis area, so you don't need the clip=false option anymore. \addplot has some additional advantages, e.g. adding legend entries with \addlegendentry{} as shown in the following example:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.11}

\tikzset{
    declare function={
%        myslope2(\x) = 24 - \x*(24/48);
        myslope(\x) = 8 - \x*(8/32);
        mycomplexfunction(\x) = sin(60*pow(\x,1.3)) + 6;
    },
}

\begin{document}
    \begin{frame}[fragile,t]
        \frametitle{1}
        \begin{tikzpicture}[scale=.9, transform shape]
            \begin{axis}[
                axis lines=center,
                axis line style={black, thick,-latex},
                axis y line=left,axis x line=bottom,
                tick style={line width=.04cm, color=black, line cap=round},
                font=\normalsize,
                color=black,
                xmin=0,
                xmax=64,
                xtick={0,16,32},
                xticklabels={0,16,32},
                ymin=0,
                ymax=16,
                ytick={4,8},
                yticklabels={4,8},
                tickwidth=.2cm,
                xlabel={P},
                xlabel style={right},
                ylabel={M},
                ylabel style={above},
                xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
                yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
                samples=200
            ]
                \addplot[ultra thick,blue,samples at={0,16,32},mark=*,mark options={black}] {myslope(x)};
                \addlegendentry{Simple slope};
                \draw[thick,black,densely dotted] (0,{myslope(16)}) -- (16,{myslope(16)}) node [black, xshift=.46cm, yshift=.05cm] {A} -- (16,0);

                \addplot[thin,red,samples=200,smooth,domain=0:32] {mycomplexfunction(x)};
                \addlegendentry{Complex function};
                \draw[thin,red,densely dotted] (0,{mycomplexfunction(20)}) -- (20,{mycomplexfunction(20)}) -- (20,0);
            \end{axis}
        \end{tikzpicture}
    \end{frame}
\end{document}

enter image description here

Max
  • 9,733
  • 3
  • 28
  • 35
  • 2
    Note that if you have \pgfplotsset{compat=1.11} (or some higher version number), then axis cs: is default, and doesn't have to be given explicitly. And if you use \addplot[ultra thick,blue,samples at={0,16,32},mark=*,mark options={black}] {myslope(x)}; instead of \draw plot, then the marks won't be clipped, so you don't need clip=false. – Torbjørn T. Jul 11 '18 at 09:01
  • @TorbjørnT. Thanks I did not know that, I've adjusted the answer accordingly. – Max Jul 11 '18 at 09:15
2

Yes. You just need to say

\draw [thick, black, densely dotted] (A -| 0,0) -- (A) -- (A |- 0,0) ;

Complete MWE.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
 tick style={line width=.04cm, color=black, line cap=round},
 font=\normalsize,color=black,
 xmin=0,xmax=64,
 xtick={0,16,32},xticklabels={0,16,32},
 ymin=0,ymax=16,
 ytick={4,8},yticklabels={4,8},
 tickwidth=.2cm,
 xlabel={P}, xlabel style={below},
 ylabel={M}, ylabel style={left},
 xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
 yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
 samples=200]
\end{axis}
\draw [ultra thick,blue] (0,2.84) -- +(-40:4.44cm) 
node [pos=.5,circle, draw, black, fill=black, scale=0.5,label=right:A] (A){} 
node [pos=0,circle, draw, black, fill=black, scale=0.5]{}  
node [pos=1,circle, draw, black, fill=black, scale=0.5]{};
\draw [thick, black, densely dotted] (A -| 0,0) -- (A)
-- (A |- 0,0) ;
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • The problem is that I located the position of point A according to the tick marks locations. It just happened that it is in the middle of the line. So I need to start drawing from the tick marks location. – Hany Jul 10 '18 at 16:19
  • Please refer to the drawing I uploaded to my question. There are other points related to the tick marks, and I need to locate their positions according to the tick mark positions. So I need to start drawing from the tick marks location, not vice versa. – Hany Jul 10 '18 at 16:26