2

I am trying to draw a picture of a concave function. I would like the axes to start at (0,0), however when I draw the picture as you can see below the y-axis starts below 0.

enter image description here

I have tried running several codes from previous answers but none of them seem to work. The last one I have tried is this one trying to override values outside the picture:

\documentclass[usenames,dvipsnames]{beamer}

\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\pgfplotsset{overwrite option/.style args={#1 with #2}{#1=#2,#1/.code=}}

\begin{document}

\begin{frame}{CONVEX AND CONCAVE FUNCTIONS}
\framesubtitle{DEFINITION}
\textbf{CONCAVE FUNCTION:} is a function where no line segment joining two points on the graph lies above the graph at any point.

{ % begin outer pgfplotsset scope
\pgfplotsset{ymax=5, overwrite option=ymin with 0, overwrite option=xmin with 0, execute at begin axis={\pgfplotsset{width=5cm}}}% the ymin key doesn't work anymore

    \begin{tikzpicture}
        \begin{axis}[
            xmax = 10,
            xtick={0,2,4,6,8,10},
            ytick={0,2,4,6},
            axis lines = left,
            domain=0:10,
            % minor y tick num=1,
            samples=100,
            % enlarge x limits=false,
            % grid=both,
            no markers,
            axis equal]
            \addplot[ultra thick,Mahogany]{2*x^(1/2)};
        \end{axis}
    \end{tikzpicture}
} % end outer pgfplotsset scope
\end{frame}

\end{document}
3
  • 3
    It's because of axis equal. Commented Aug 24, 2019 at 10:39
  • pfffffff don't believe is working, many many many thanks for this. Yesterday I was doing ma head in quite a while because if I removed that feature the document wouldn't render thinking, poor of me, that it wouldn't hurt having it there Commented Aug 24, 2019 at 13:54
  • You can still get the correct output with axis equal, but then you need to change the width/height of the axis. Do you want/need the axis equal feature? (I.e. that 1 unit on the x-axis is the same length as 1 unit on the y-axis.) Commented Aug 24, 2019 at 18:35

2 Answers 2

4

I would write your frame on the following way:

\documentclass[usenames,dvipsnames]{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.16}
%\pgfplotsset{overwrite option/.style args={#1 with #2}{#1=#2,#1/.code=}}

\begin{document}

\begin{frame}
\frametitle{CONVEX AND CONCAVE FUNCTIONS}
\framesubtitle{DEFINITION}
\textbf{CONCAVE FUNCTION:} is a function where no line segment joining two points on the graph lies above the graph at any point.

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[width=5.5cm,
            xmax=11, ymax = 7,
            xtick={0,2,...,10},
            axis lines = left,
            no markers,
            samples=100,
        every axis plot post/.append style={ultra thick,Mahogany}]
    \addplot[domain=0:10]{2*x^(1/2)};
        \end{axis}
    \end{tikzpicture}
\end{center}
\end{frame}

\end{document}

enter image description here

1

This is plain TikZ version of Zarko's answer.

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\frametitle{CONVEX AND CONCAVE FUNCTIONS}
\framesubtitle{DEFINITION}
\textbf{CONCAVE FUNCTION:} is a function where no line segment joining two points on the graph lies above the graph at any point.

\begin{center}
\begin{tikzpicture}[scale=.5,>=stealth]
\draw[->] (-.3,0)--(11,0);
\draw[->] (0,-.3)--(0,7);
\foreach \i in {0,2,...,10} \draw (\i,0)--+(90:2mm)--+(-90:2mm) node[below]{$\i$};
\foreach \j in {0,2,4,6} \draw (0,\j)--+(0:2mm)--+(180:2mm) node[left]{$\j$};
\draw[cyan,thick,smooth] plot[domain=0:10,samples=200] (\x,{2*\x^(1/2)});
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

Update: (I am not going to dislike any package.)
Using plain TikZ or pgfplots is matter of taste. I believe that with long and detail documentation, pgfplots did a good job. To use pgfplots, you have to learn many many new keywords, and accepted auto-scaling, auto-labeling, etc. I like to scale graphs and put labels myself: why would I want to be auto-scaled meanwhile I want to care about every single path, every single label ? Therefore, I intend to use plain TikZ as long as it can draw well. This is my experience after drawing hundreds of graphs in Calculus, Differential Equations, Analytic Geometry, .... For beginners, I recommend plain TikZ because it use just basic commands of TikZ.

The following is a comparison of 2 drawings via pgfplots and plain TikZ: auto-scaling that pgfplots implicitly used is [xscale=.31,yscale=.39]. Is there any good reason for these numbers?

enter image description here

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=5cm,
            xmax=11, ymax = 7,
            xtick={0,2,...,10},
            axis lines = left,
            no markers,
            samples=20,
            every axis plot post/.append style={thick,red}]
\addplot[domain=0:2 ]{2*x^(1/2)};
\addplot[domain=2:10]{2*x^(1/2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[xscale=.31,yscale=.39,>=stealth]
\draw[->] (-.3,0)--(11,0);
\draw[->] (0,-.3)--(0,7);
\foreach \i in {0,2,...,10} \draw (\i,0)--+(90:2mm)--+(-90:2mm) node[below]{$\i$};
\foreach \j in {0,2,4,6} \draw (0,\j)--+(0:2mm)--+(180:2mm) node[left]{$\j$};
\draw[cyan,thick,smooth] plot[domain=0:10,samples=200] (\x,{2*\x^(1/2)});
\end{tikzpicture}    
\end{document}
4
  • 1
    Thanks for this Black Mild, I see that the picture is exactly the same as the previous answer but with an utterly different code, which one is more "propper"? Commented Aug 24, 2019 at 15:45
  • 1
    @RubénPérezSanz I don't think one is more proper than the other. In general for function plots though, using pgfplots as you were doing in the first place is often a bit more convenient because pgfplots builds the axis for you, and takes care of scaling. Commented Aug 24, 2019 at 18:33
  • I added an update concerning with comments above
    – Black Mild
    Commented Aug 25, 2019 at 9:51
  • Regarding the last question in your edited answer: pgfplots sets the width/height of the axis, and scales the plot accordingly. Commented Aug 25, 2019 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .