TransWikia.com

Control labeling in tikz

TeX - LaTeX Asked on April 3, 2021

Is there an efficient way to control the labels of tikiz circuit when labeling is uniform between 0:0.125:…:2, I want to show the number 0,0.125,..2 in the x-axis

documentclass[border=1cm]{standalone} 
    usepackage{amsmath}
    usepackage[siunitx]{circuitikz}
    usetikzlibrary{angles, arrows.meta,quotes}   
    usepackage{pgfplots}
    pgfplotsset{compat=newest}
begin{document}
    begin{tikzpicture}
        begin{axis}[
            grid=both,
            minor tick num=17,
            grid style={line width=.1pt, draw=gray!10},
            major grid style={line width=.2pt,draw=gray!50},
            axis lines=middle,
            enlargelimits={abs=0.2},
            xmin = 0, xmax = 2,
            ymin = -1, ymax = 7,
            xtick={0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1}
            xticklabels={$0$,$0.125$,$0.25$,$0.375$,$0.5$,$0.625$,$0.75$,$0.875$,$1$}
        ]
            
            addplot[domain=0:2,samples=50,smooth,red] {4*cos(deg(2*pi*x-2*pi*0.125))+3};
   
        end{axis}
    end{tikzpicture}

end{document}

One Answer

enter image description here

  • font size used in labels should be reduced
  • precision of numbers had to be increased (from default) 2 to 3 decimals
  • the image should be wide enough for the labels that they will not overlap
documentclass[border=1cm]{standalone}
usepackage{pgfplots}
pgfplotsset{compat=1.17}

begin{document}
    begin{tikzpicture}
begin{axis}[
    width=15cm,
    axis lines=middle,
    grid=both,
    minor tick num=4,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
    enlargelimits={abs=0.2},
    xmin = 0, xmax = 2,
    ymin = -1, ymax = 7,
    xtick={0,0.125,...,2},
    x tick label style={font=scriptsize,
                        /pgf/number format/precision=3}
            ]
    addplot[domain=0:2,samples=50,smooth,red] {4*cos(deg(2*pi*x-2*pi*0.125))+3};
end{axis}
    end{tikzpicture}
end{document}

Edit: If it is acceptable for you to rotate xtick labels, for example for 45 degrees, you can then increase font size used in xtick labels. In this case x tick label style is:

    x tick label style={font=small, rotate=45, anchor=east,
                        /pgf/number format/precision=3,}

and resulted image is:

enter image description here

Correct answer by Zarko on April 3, 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