2

Using the code in the answer in this question, the attached top diagram is generated. Can the code be modified so that the red node B* could be drawn outside the plotted line, with the dotted line extending from x=27 to y=13; as shown in the attached bottom diagram. Also can the labels of the individual tick marks x=27 be shifted to the right, and y=13 be shifted up.

    \documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\tikzset{
declare function={
    myslope(\x) = 24 - \x*(24/48);
    mycomplexfunction(\x) = sin(60*pow(\x,1.2)) + 6;
},
}
\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,12,24,27,48}, xticklabels={0,12,24,27,48},
 ymin=0, ymax=30,
 ytick={12,13,18,24}, yticklabels={12,13,18,24},
 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,12,24,27,48},mark=*,mark options={black}] (axis cs:\x,{myslope(\x)});
\draw[thick,black,densely dotted] (axis cs:0,{myslope(12)}) -- (axis cs:12,{myslope(12)}) node [black, xshift=.46cm, yshift=.05cm] {A} -- (axis cs:12,0);
\draw[thick,green,densely dotted] (axis cs:0,{myslope(24)}) -- (axis cs:24,{myslope(24)}) node [green, xshift=.46cm, yshift=.05cm] {B} -- (axis cs:24,0);
\draw[thick,red,densely dotted] (axis cs:0,{myslope(27)}) -- (axis cs:27,{myslope(27)}) node [red, xshift=.46cm, yshift=.05cm] {B*} -- (axis cs:27,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
Hany
  • 4,709

2 Answers2

4

If you want the green dotted line drawn to a different y-coordinate, use a different y-coordinate ... I.e. use 13 instead of myslope(27).

That doesn't move the dot of course, so I used a second \addplot to create that one. (I changed \draw ... plot to \addplot, makes more sense to me inside an axis environment.)

For the ticklabels, you could make use of extra ticks.

enter image description here

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{pgfplots} % loads tikz
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\tikzset{
declare function={
    myslope(\x) = 24 - \x*(24/48);
    mycomplexfunction(\x) = sin(60*pow(\x,1.2)) + 6;
},
}
\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},
 xmin=0, xmax=64,
 xtick={0,12,24,48}, 
 ymin=0, ymax=30,
 ytick={12,18,24}, 
 tickwidth=.2cm,
 xlabel={P}, xlabel style={above}, % changed right to above
 ylabel={M}, ylabel style={right}, % changed above to right
 xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
 yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
 % specify extra ticks and modifications to their style
 extra x ticks={27},
 extra y ticks={13},
 extra x tick style={xticklabel style={xshift=5pt}},
 extra y tick style={yticklabel style={yshift=5pt}}
]

\addplot [ultra thick, blue, samples at={0,12,24,48},mark=*,mark options={black}] {myslope(x)};
\addplot [ultra thick, mark=*] coordinates {(27,13)};

\draw[thick,black,densely dotted] (axis cs:0,{myslope(12)}) -| node [above right] {A}(axis cs:12,0);
\draw[thick,green,densely dotted] (axis cs:0,{myslope(24)}) -| node [below left] {B}  (axis cs:24,0);
\draw[thick,red,densely dotted] (axis cs:0,13) -| node [above right] {B*} (axis cs:27,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

In the code above I removed the xticklabels and yticklabels, as they didn't seem necessary. On the other hand, when manually specifying them, you can use that to move some of them around a bit. For example xticklabels={0,12,\llap{24},\rlap{27},48},...,yticklabels={\raisebox{-10pt}{12},\raisebox{5pt}{13},18,24}.

enter image description here

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{pgfplots} % loads tikz
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\tikzset{
declare function={
    myslope(\x) = 24 - \x*(24/48);
    mycomplexfunction(\x) = sin(60*pow(\x,1.2)) + 6;
},
}
\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},
 xmin=0, xmax=64,
 xtick={0,12,24,27,48}, xticklabels={0,12,\llap{24},\rlap{27},48},
 ymin=0, ymax=30,
 ytick={12,13,18,24},  yticklabels={\raisebox{-10pt}{12},\raisebox{5pt}{13},18,24}, 
 tickwidth=.2cm,
 xlabel={P}, xlabel style={above},
 ylabel={M}, ylabel style={right},
 xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
 yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
]

\addplot [ultra thick, blue, samples at={0,12,24,48},mark=*,mark options={black}] {myslope(x)};
\addplot [ultra thick, mark=*] coordinates {(27,13)};

\draw[thick,black,densely dotted] (axis cs:0,{myslope(12)}) -| node [above right] {A}(axis cs:12,0);
\draw[thick,green,densely dotted] (axis cs:0,{myslope(24)}) -| node [below left] {B}  (axis cs:24,0);
\draw[thick,red,densely dotted] (axis cs:0,13) -| node [above right] {B*} (axis cs:27,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

Take 3

As Zarko pointed out in a (now deleted) comment, if you have \pgfplotsset{compat=1.11}, or with a higher version number, then axis cs: is default for \draw and the like inside an axis, so it doesn't have to be specified explicitly. I also removed the axis x line/axis y line styles, instead using axis lines=left and setting the position of xlabel/ylabel manually. I suppose use whatever method you prefer. Another minor thing, I used ticklabel shift instead of the more cumbersome x(y)ticklabel style with y(x)shift in your original code.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.11} % among other things, makes axis cs: default
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\tikzset{
declare function={
    myslope(\x) = 24 - \x*(24/48);
}
}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[
 axis lines=left, % instead of center, removed the othe axis x/y line options
 axis line style={black, thick,-latex},
 tick style={line width=.04cm, color=black, line cap=round},
 xmin=0, xmax=64,
 xtick={0,12,24,27,48},
 xticklabels={0,12,\llap{24},\rlap{27},48},
 ymin=0, ymax=30,
 ytick={12,13,18,24},
 yticklabels={\raisebox{-10pt}{12},\raisebox{5pt}{13},18,24}, 
 tickwidth=.2cm,
 xlabel={P},
 ylabel={M},
 every axis x label/.style={at={(1,0)},above}, % manually position xlabel
 every axis y label/.style={at={(0,1)},right}, % manually position ylabel
 ticklabel shift=2pt % move all ticklabels away from axis
]

\addplot [ultra thick, blue, samples at={0,12,24,48},mark=*,mark options={black}] {myslope(x)};
\addplot [ultra thick, mark=*] coordinates {(27,13)};

\draw[thick,black,densely dotted] (0,{myslope(12)}) -| node [above right] {A}  (12,0);
\draw[thick,green,densely dotted] (0,{myslope(24)}) -| node [below left]  {B}  (24,0);
\draw[thick,red,densely dotted]   (0,13)            -| node [above right] {B*} (27,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
Torbjørn T.
  • 206,688
  • 1
    @Zarko I do of course know the bit about axis cs. Regarding axis lines=center it does actually influence the output, in that it changes the position of the xlabel and ylabel. (Which of course could be modified elsewhere, but I just left it unchanged instead.) – Torbjørn T. Jul 11 '18 at 08:57
0

I never used the beamer class, so I tried this in article mode.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfkeys}
\begin{document}
        \begin{tikzpicture}[scale=0.2]
            \draw[thick, blue] (0,24) -- (48,0);
            \draw[very thick,->,>=latex] (0,0) -- (55,0);
            \draw[very thick,->,>=latex] (0,0) -- (0,30);
            \filldraw[thick,densely dotted] (0,18) -- (12,18) node[above right]{$A$} circle (0.35)-- (12,0);
            \filldraw[thick,densely dotted,red](0,13) -- (27,13)node[above right]{$B*$} circle (0.35) -- (27,0);
            \filldraw[thick,densely dotted](0,12) -- (24,12)node[above right]{$B$} circle (0.35) -- (24,0);
        \end{tikzpicture}
\end{document}

It seems to provide what you want if I understood correctly. You can add the ticks on the axis easily.

  • A fine manual drawing; but I needed to make the tick marks as reference points, which is answered by the following answer. Thank you anyway. – Hany Jul 11 '18 at 09:52