3

I want to draw the following graph using PGF-Tikz:

enter image description here

Using the following codes:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}
    
\begin{tikzpicture}[>=latex]
    
    \begin{axis}[mystyle/.style={semithick},        
        axis x line=center,
        axis y line=center,
        xtick={0,1,2,4,8},
        extra y ticks={3.5},
        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=north,below,yshift=0.5ex},
        yticklabel style={red},
        extra y tick style={yticklabel style={color={black}}},
        xmin=-1,
        xmax=10,
        ymin=-5,
        ymax=4.5]
        % Main lines
        \addplot[mystyle,green,thick]coordinates{(0,3.5)(1,0.5)(2,0)};
        \addplot[mystyle,orange,thick]coordinates{(2,0)(4,0)};
        \addplot[mystyle,thick]coordinates{(4,0)(8,2)};
        % vertical dashed lines
        \addplot[mystyle,dashed]coordinates{(8,2)(8,0)};
    \end{axis}
\end{tikzpicture}
    
\end{document}

I got the following picture-

enter image description here

I don't know how to remove the the numbers on vertical axis, i.e., I want to remove the labels or numbers -2,2,4,-4 on vertical axis.

I also want to increase gaps between the numbers on horizontal axis.

Kindly help.

Please make correction of my code.

Edit: The following code of @Ronald produce the following graph: enter image description here

But I want to delete the horizontal cut on the vertical axis.

3

Like this:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}}


\begin{document}
    
\begin{tikzpicture}[>=latex]
    
    \begin{axis}[mystyle/.style={semithick},        
        axis x line=center,
        axis y line=center,
        xtick={0,1,2,4,8},
        extra x ticks={0,0}, %adds tick
        extra x tick style={ xticklabel style={below right}}, %moves 0 a little to the right so it does not collide the y axis.
        extra y ticks={3.5},
        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=north,below,yshift=0.5ex},
        yticklabels={,,} %this here I changed
        extra y tick style={yticklabel style={color={black}}},
        xmin=-1,
        xmax=10,
        ymin=-5,
        ymax=4.5]
        % Main lines
        \addplot[mystyle,green,thick]coordinates{(0,3.5)(1,0.5)(2,0)};
        \addplot[mystyle,orange,thick]coordinates{(2,0)(4,0)};
        \addplot[mystyle,thick]coordinates{(4,0)(8,2)};
        % vertical dashed lines
        \addplot[mystyle,dashed]coordinates{(8,2)(8,0)};
    \end{axis}
\end{tikzpicture}
    
\end{document}

enter image description here

4
  • Thanks but how to delete the horizontal-cut on the vertical axis at those 4 points ? Dec 14 '20 at 7:11
  • 1
    Hello! I edited the answer. You have to add \pgfplotsset{ytick style={draw=none}}
    – Roland
    Dec 14 '20 at 7:23
  • Thank you very much. It solved the problem. One final request- How to add the number 0 on the horizontal axis Dec 14 '20 at 7:55
  • Add \extra x ticks={0,0}, see edit
    – Roland
    Dec 14 '20 at 8:21
3

Ticks on y-axis you can remove by ytick=\empty. In MWE below is also removed all clutter in your code.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} % <---


\begin{document}
    \begin{tikzpicture}
\begin{axis}
    axis lines=center, % <---
    xtick={1,2,4,8},
    extra x ticks={0},  % <---
    ticklabel style={fill=white,font=\footnotesize}, % <---    
    ytick=\empty, % <--- remove yticks
    extra y ticks={3.5},
    extra y tick labels={$\nu_p(\alpha)$},
    xlabel={$n$},
    ylabel={$\nu_2(a_n)$},
    xlabel style={right},
    ylabel style={above},
    xmin=-1,
    xmax=10,
    ymin=-4.5,
    ymax=4.5,
    every axis plot post/.append style={thick}% thickness of plotted lines
    ]
    % Main lines
\addplot[green]  coordinates {(0,3.5)(1,0.5)(2,0)};
\addplot[orange] coordinates {(2,0)(4,0)};
\addplot[black]  coordinates {(4,0)(8,2)};
% vertical dashed lines
\draw[dashed] (8,2) -- (8,0);
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Edit: added x tick label at zero point, as OP asked in his comment.

4
  • thanks. How to add 0 on the horizontal axis in the same level ? Dec 14 '20 at 8:12
  • 1
    with extra x tick. I will add this asap.
    – Zarko
    Dec 14 '20 at 8:14
  • thank you very much Dec 14 '20 at 8:15
  • @M.A.SARKAR, see edited answer.
    – Zarko
    Dec 14 '20 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.