控制 tikz 中的標籤

控制 tikz 中的標籤

當標籤在 0:0.125:...:2 之間統一時,有沒有一種有效的方法來控制 tikiz 電路的標籤,我想在 x 軸上顯示數字 0,0.125,..2

\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}

答案1

在此輸入影像描述

  • 應減小標籤中使用的字體大小
  • 數字精度必須增加(從預設值)2 到 3 位小數
  • 圖像對於標籤來說應該足夠寬,以免它們重疊
\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}

編輯: 如果您可以接受旋轉xtick標籤(例如 45 度),則可以增加xtick標籤中使用的字體大小。在本例中,x 刻度標籤樣式為:

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

結果影像是:

在此輸入影像描述

相關內容