pi(또는 pi/2) 단위로 축에 레이블을 지정하고 보조 눈금 번호를 사용합니다.

pi(또는 pi/2) 단위로 축에 레이블을 지정하고 보조 눈금 번호를 사용합니다.

를 사용하여 pgfplots가로축 주요 눈금을 pi또는 pi/2, 또는 단위로 표시하고 싶은 경우가 많습니다 pi/3. minor tick num = <value>작은 눈금의 수를 지정하기 위해 이러한 "삼각법 주요 눈금"을 사용할 수 있습니까 ?

현재 "삼각법 주요 눈금"에 대한 나의 접근 방식 은 및 를 wholes지정하는 스타일을 만드는 것입니다 . -4*pi ~ +4*pi 범위에 대한 눈금을 지정하고 필요에 따라 그래프를 자릅니다. 마찬가지로 파이의 반정수 배수를 요구 하는 스타일이 있습니다 .xtick=<integer multiples of pi>xticklabels=<labels>halves

\documentclass{standalone}

\usepackage{pgfplots} %This package also loads tikz

\pgfplotsset{
    % Global Styles
    axis lines = middle,
    xlabel = $x$,
    ylabel = $y$,
    no markers,
    samples=50,
    grid = both,
    trig format plots=rad,
    enlargelimits = false,
    axis line style = {line width=0.5pt},
    every axis plot/.append style={
        line width = 1.25pt,
        smooth,
        },
    % Label every pi
    halves/.style={
    xtick = {-12.5664, -10.9956, -9.42478, -7.85398, -6.28319, -4.71239, -3.14159, -1.5708, 0, 1.5708, 3.14159, 4.71239, 6.28319, 7.85398, 9.42478, 10.9956, 12.5664, 14.1372, 15.708, 17.2788, 18.8496, 20.4204, 21.9911, 23.5619, 25.1327},
    xticklabels = {$-4\pi$,$-\frac{7\pi}{2}$,$-3\pi$,$-\frac{5\pi}{2}$,$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$,$\frac{5\pi}{2}$,$3\pi$,$\frac{7\pi}{2}$,$4\pi$,$\frac{9\pi}{2}$,$5\pi$,$\frac{11\pi}{2}$,$6\pi$,$\frac{13\pi}{2}$,$7\pi$,$\frac{15\pi}{2}$,$8\pi$}
    },
    % Label every pi
    wholes/.style={
    xtick = {-12.5664, -9.42478, -6.28319, -3.14159, 0., 3.14159, 6.28319, 9.42478, 12.5664, 15.708, 18.8496, 21.9911, 25.1327},
    xticklabels = {$-4\pi$,$-3\pi$,$-2\pi$,$-\pi$,$0$,$\pi$,$2\pi$,$3\pi$,$4\pi$,$5\pi$,$6\pi$,$7\pi$,$8\pi$}
    }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[wholes,domain=-pi:3*pi,ymin=-2,ymax=+4,minor tick num = 1]
    \addplot {1-2*cos(2*x)};
    \legend{$1-2\cos(2x)$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[halves,domain=-pi:pi,ymin=-2,ymax=+4,minor tick num = 1]
    \addplot[red] {2+0.5*sin(4*x)};
    \legend{$2+0.5\sin(4x)$}
\end{axis}
\end{tikzpicture}

\end{document} 

출력 포함

여기에 이미지 설명을 입력하세요

문제:minor tick num=1x축에서 작동하지 않는다는 점을 제외하면 이 솔루션에 매우 만족합니다 . 반올림 오류 때문인 것 같습니다.틱을 수동으로 지정할 때 pgfplot의 보조 그리드가 표시되지 않습니다.

물론 작은 틱을 수동으로 지정할 수도 있습니다. 난 정말 이런 일을 하고 싶지 않다---이런 그래프를 많이 만들어야 할 때도 있고, 싶을 때도 있고 minor tick num = 1, 싶을 때도 있다 minor tick num = 3.

더 나은 접근 방식이 있습니까?

답변1

내 솔루션을 사용하여 주어진여기minor tick num원하는 만큼 작은 눈금을 쉽게 추가할 수 있습니다 .

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        axis lines=middle,
        xlabel=$x$,
        ylabel=$y$,
        no markers,
        samples=50,
        trig format plots=rad,
        every axis plot/.append style={
            line width=1.25pt,
            smooth,
        },
    }
    % define precision of \pi
    % this is set here to the value of \pgfmathpi
    \pgfmathsetmacro{\PI}{3.141592654}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            domain=-0.5*pi:1.5*pi,
            ymin=-1.5,
            ymax=+1.5,
            %
            % scale x axis values by \pi and
            % remove the corresponding label
            scaled x ticks={real:\PI},
            xtick scale label code/.code={},
            % in case you want to set an explicit tick distance
            xtick distance=\PI/2,
            % add code here for formatting the `xlabels'
            % I configured exceptions for \pm\pi where no number in front
            % of these are shown and for fractional values these should be
            % shown as fractions
            xticklabel={%
                \ifdim \tick pt = 1 pt
                    \strut$\pi$%
                \else\ifdim \tick pt = -1 pt
                    \strut$-\pi$%
                \else
                    % to avoid some mess with TeX precision, first
                    % round the \tick value to one digit after the comma
                    \pgfmathparse{round(10*\tick)/10}
                    % depending on whether the resulting number is an integer
                    % show it as integer only, otherwise use the style given
                    % in `xticklabel style'
                    \pgfmathifisint{\pgfmathresult}{%
                        \strut$\pgfmathprintnumber[int detect]{\pgfmathresult}\pi$%
                    }{%
                        \strut$\pgfmathprintnumber{\pgfmathresult}\pi$%
                    }
                \fi\fi
            },
            % set number plotting to frac style
            xticklabel style={
                /pgf/number format/frac,
                /pgf/number format/frac whole=false,
            },
            % add minor thicks
            minor tick num=1,
        ]
            \addplot {sin(2*x)};
        \end{axis}
    \end{tikzpicture}
\end{document}

위 코드의 결과를 보여주는 이미지

참고: pgfplots 1.13 이상이 필요합니다.

관련 정보