TransWikia.com

Help me to draw the figure using tikzpgf plot

TeX - LaTeX Asked on June 23, 2021

How to draw the following figure using Tikzpgf ?

enter image description here

I need to plot with the axes exactly in the figure. Also I want to display the vertex (4,-2).

I know to join two points (a,b) and (c,d) by straightline, we use the code

begin{tikzpicture} 
draw (a,b)--(c,d);
end{tikzpicture}

Here in the picture, we have to join

(0,1) to (1,0), (1,0) to (2,-1), (2,-1) to (4,-2), (4,-2) to (8,-3) and $(0, v_p(alpha)$ to (1,0).

I don’t want axes like given below

enter image description here

Please help me to plot the figure.

Also tell me which package should I add with my latex editor to draw pgf plot?

2 Answers

With pure TikZ:

documentclass[tikz, margin=3.14159 mm]{standalone}
usetikzlibrary{arrows.meta}

begin{document}
    begin{tikzpicture}
% axis
draw[-Straight Barb] (-1,0) -- (9,0) node[right] {$n$};
draw[-Straight Barb] (0,-3) -- (0,3) node[above] {$v_2(a_n)$};
% ticks and dashed lines, drawn in loop
foreach i [count=j from 0] in {1,2,4,8}
    draw[densely dashed] (i,0) node [above] {i} -- (i,-j) coordinate (xj);
foreach i/j/k in {-1/-1/3, 0/0/2, 1/1/1, 2/$v_p(alpha)$/0}
path[draw=red, dashed] (0,i) node[left,fill=white, inner sep=2pt] {j} -- (xk);
% function
draw[thick, green]  plot coordinates {(0,2) (1,0) (2,-1)};
draw[thick,   red]  plot coordinates {(2,-1) (4,-2)  (8,-3)};
    end{tikzpicture}
end{document}

enter image description here

Code you can compile in your local installation of LaTeX or using Overleaf service.

Edit:

Above MWE works independent from the used documentclass. For example, if you use the amsart package, than the firs code line

documentclass[tikz, margin=3.14159 mm]{standalone}

replace with

documentclass[12pt,leqno]{amsart}
usepackage{tikz}

Result is the very same as before, with no errors nor warning nor bad box messages.

In the case, that you like to include above diagram as figure with caption in your document, you can insert it in figure float environment and add caption as follows:

documentclass[12pt,leqno]{amsart}
usepackage{pgfplots}       % it also load tikz package
pgfplotsset{compat=1.17}   
usetikzlibrary{arrows.meta}

usepackage{caption}
usepackage[colorlinks=true,
            citecolor={black}]{hyperref}

usepackage{lipsum} % added for generating a dummy text, 
                    % not needed in real document
begin{document}
lipsum[1]  % dummy text
begin{figure}[ht] % placed here or on the top of page
    begin{tikzpicture}
% axis
draw[-Straight Barb] (-1,0) -- (9,0) node[right] {$n$};
draw[-Straight Barb] (0,-3) -- (0,3) node[above] {$v_2(a_n)$};
foreach i [count=j from 0] in {1,2,4,8}
    draw[densely dashed] (i,0) node [above] {i} -- (i,-j) coordinate (xj);
foreach i/j/k in {-1/-1/3, 0/0/2, 1/1/1, 2/$v_p(alpha)$/0}
path[draw=red, dashed] (0,i) node[left,fill=white, inner sep=2pt] {j} -- (xk);
% function
draw[thick, green]  plot coordinates {(0,2) (1,0) (2,-1)};
draw[thick,   red]  plot coordinates {(2,-1) (4,-2)  (8,-3)};
    end{tikzpicture}
caption{Caption of the figure} % caption
label{fig:diagram}             % for referencing of figure, key select as you wish
end{figure}
lipsum[2]  % dummy text

For further explanation see figure ref{fig:diagram} dots % referencing of figure
end{document}

Which gives the following result (showed is a part of page):

enter image description here

As expected, the compilation of this MWE is also error-free.

Correct answer by Zarko on June 23, 2021

And with tikz and pgfplots:

documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=newest}
begin{document}
begin{tikzpicture}[>=latex]
begin{axis}[mystyle/.style={semithick},
  axis x line=center,
  axis y line=center,
  xtick={0,1,2,4,8},
  ytick={-1,1},
  extra y ticks={3},
  extra y tick labels={$nu_p(alpha)$},
  xlabel={$n$},
  ylabel={$nu_2(a_n)$},
  xlabel style={right},
  ylabel style={above},
  x tick label style={anchor=south,above,yshift=1ex},
  yticklabel style={red},
  extra y tick style={yticklabel style={color={black}}},
  xmin=-1,
  xmax=9,
  ymin=-4,
  ymax=4]
% Main lines
addplot[mystyle,green,thick]coordinates{(0,3)(1,0)(2,-1)};
addplot[mystyle,orange,thick]coordinates{(2,-1)(4,-2)};
addplot[mystyle,thick]coordinates{(4,-2)(8,-3)};
% red dashed help lines
addplot[mystyle,dashed,red]coordinates{(0,1)(1,0)};
addplot[mystyle,dashed,red]coordinates{(0,0)(2,-1)};
addplot[mystyle,dashed,red]coordinates{(0,-1)(4,-2)};
% vertical dashed lines
addplot[mystyle,dashed]coordinates{(2,0)(2,-1)};
addplot[mystyle,dashed]coordinates{(4,0)(4,-2)};
addplot[mystyle,dashed]coordinates{(8,0)(8,-3)};
node at (0,0) [above,anchor=south west,shift={(-0.075cm,0.075cm)}] {0};
end{axis}
end{tikzpicture}
end{document}

enter image description here

Answered by Matthias Arras on June 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP