# Plot graph with labels

How can I draw the following graphs?

I wanted to do this with Python or Matlab, but I heard that this also works with LaTeX.

• Jun 16 at 19:42
• Have you said with Python? No idea of Matlab, with you can use also in the same way Octave, or R (the default engine). In any case, will need to have R and knitr library to compile.
– Fran
Jun 17 at 8:44
• 2 figures are unrelated to each other. It should be in 2 separated questions Jun 17 at 12:29

This is a long comment with my suggestion of using Asymptote (can be imbbed into LaTeX).

For the first figure, there is a lack of information of the function P(t). What is the analytic formula of it? All I can guess it is a solution of some logistic differential equation.

For the second figure, Asymptote has nice routine dir for tangency. You can adapt the following similar figure that I used in my teaching.

real t=.37;                 // time along a path
pair P=point(g,t);          // P is the point of time t along path g
pair Pt=dir(g,t);           // Pt is the unit tangent at P
path T=P-3Pt--P+7.5Pt;      // to draw tangent segment passing P


// http://asymptote.ualberta.ca/
unitsize(1cm);
size(10cm);
pair A=(0,0), B=(6,3.5);
path g=A..controls A+3dir(-60) and B+4dir(-105) .. B;
draw(g,black+.8pt);
real t=.37;
pair P=point(g,t);
pair Pt=dir(g,t);
path T=P-3Pt--P+7.5Pt;
draw(Label("Tangent line",Relative(.6),align=SE),T,magenta+.6pt);
pen penq=.5cyan+.5black;
pen pent=magenta;
pair[] Q;
real[] a={1,.92,.8,.68,.5};
real[] Qforward={0,.2,.25,.25,1};
string[] sQ={"$Q_0$","$Q_1$","$Q_2$","$Q_3$","$Q_4$"};
string[] sm={"$m_0$","$m_1$","$m_2$","$m_3$","$m_4$"};
for(int i=0;i<sm.length;++i)
sm[i]="Slope "+sm[i];
for(int i=1;i<5;++i){
Q[i]=relpoint(g,a[i]);
path Tq=-.3(Q[i]-P)+P--Q[i]+Qforward[i]*(Q[i]-P);
draw(Label(sm[i],align=E,EndPoint),Tq,dashed+penq);
dot(sQ[i],align=E,Q[i]);
}
dot("$P$",align=S,P);
label(minipage("Slope $m=$\\instantaneous rate\\of change at $P$",4cm),
align=2E+S,point(T,1),pent);
pair pS=point(P--Q[1],.5);
draw(Label("Secant lines",align=2N+W,EndPoint,penq),pS+.3dir(150)--pS+2dir(150));
// shipout(bbox(5mm,invisible));

• First of all: thank you for the effort! For the first figure there is no analytic formula. It would be interesting to know if I can draw curves given some specific points and then regret on this points. Jun 20 at 13:18

Using tzplot:

\documentclass{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}[x=.1cm,y=.01cm,font=\scriptsize,label distance=-5pt]
\tzhelplines[xstep=5,ystep=50,solid,gray!25](50,350)
\tzaxes(55,400){$t$}{$p$}
\tzticksx{0,10,...,50}
\tzticksy{50,100,...,350}
\tznode(25,-50){Time (days)}[b]
\tznode(-10,200){Number of flies}[rotate=90]

\tzto[cyan,thick,out=0,in=180]"curve"(0,0)(50,300)
\tzvXpointat{curve}{23}(P)
\tzvXpointat{curve}{45}(Q)
\tzline[tzextend={30}{15}](P)(Q)
\tzlink[red,thick](P)[-|]{$\Delta t=22$}[pos=.25,b,black](Q)
\tzline[draw=none](P-|Q){$\Delta p=190$}[r](Q)
\tzdots*[cyan](P){$P$}[135](Q){$Q$}[-45];
\tznode(30,150){$\frac{\Delta p}{\Delta t}\approx 8.6$}[r]
\end{tikzpicture}

\end{document}


\documentclass{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}[font=\scriptsize]
\tzhelplines(0,0)(5,4)
% curve
\tzto[cyan,thick,out=70,in=160]"curve"(0,0)(5,3)
% points on the curve
\tzvXpointat{curve}{1}(P)
\tzvXpointat{curve}{4}(Q)
\tzdots*(P){$P$}[135](Q){$Q$}[-90];
% secants
\tzsecant{curve}(P)(Q)[0:5]{Secant}[r,blue]
\tzsecant[red!50]{curve}(P)(3.5,0)[0:4.7]
\tzsecant[red!50]{curve}(P)(3  ,0)[0:4.3]
\tzsecant[red!50]{curve}(P)(2.5,0)[0:3.9]
\tzsecant[red!50]{curve}(P)(2  ,0)[0:3.6]
\tzsecant[red!50]{curve}(P)(1.5,0)[0:3.4]{Secants}[r]
% tangent
\tztangent[red]{curve}(P)[0:3]{Tangent}[al]
\end{tikzpicture}

\end{document}

• If I compile your code, I get the following error message: Package tikz Error: I do not know the path named curve'. Perhaps you misspelt it. Sep 25 at 11:46
• @wayne I could not reproduce your problem with updated texlive. Sep 26 at 7:54
• @wayne Be sure that you use double quotes, not back quotes: "curve". Sep 26 at 9:18
• I use double quotes. The problem persists 2 days ago
• @wayne With texlive updated, I removed all the auxiliary files and tested again on mactex to compile above code with latex, pdflatex, xelatex, lualatex` and got "SUCCESS". yesterday