포인트의 x 좌표를 추가 x 틱으로 넣는 방법은 무엇입니까?

포인트의 x 좌표를 추가 x 틱으로 넣는 방법은 무엇입니까?

포인트의 x 좌표를 추가 x 틱으로 넣어야 하지만 일반적인 방법은 비합리적입니다. 포인트가 많을 수 있고 중복되는 것이 extra x ticks좋지 않기 때문입니다.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}

    \begin{document}

    %---------------------------------------------------------

            \begin{tikzpicture}[
                ]
                \begin{axis}[
                    width=\linewidth, axis lines = middle,
                    extra x ticks = {1.1, 2.2,3.3}, % irrational decision, because there can be many points
                    xtick=data,
                ]
                \addplot[] {x};
                \addplot[color=red,mark=*, only marks] coordinates {
                        (1.1,1)
                        (2.2,2)
                        (3.3,3)
                    };
                \end{axis}
            \end{tikzpicture}

    %---------------------------------------------------------
    \end{document}

답변1

만약 당신이

xtick=data, % data points from first \addplot
extra x ticks={-4,...,4} % regularly spaced ticks

첫 번째 의 각 지점에 대한 틱을 얻을 수 \addplot있고 추가로 규칙적인 간격의 틱도 얻을 수 있습니다. 그래도 조금 지저분해질 수 있습니다. 이들을 분리하는 한 가지 방법은 좌표 틱을 아래로 이동하는 것입니다. 즉

xticklabel style={yshift=-15pt},
extra x tick style={tick label style={yshift=15pt}}

두 번째 줄은 첫 번째 줄이 추가 눈금 라벨에도 영향을 미치기 때문에 필요합니다.

의 경우 옵션을 nodes near coords추가하면 해당 플롯의 점 옆에 x 값이 추가됩니다. 아래 코드는 이 두 가지를 모두 보여줍니다.point meta=x,nodes near coords\addplot

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

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}

    \begin{document}

    %---------------------------------------------------------

            \begin{tikzpicture}[
                ]
                \begin{axis}[
                    width=\linewidth, 
                    axis lines = middle,
                    xtick=data, % data points from first \addplot
                    extra x ticks={-4,...,4}, % regularly spaced ticks
                    xticklabel style={yshift=-15pt},
                    extra x tick style={tick label style={yshift=15pt}}
                ]
                \addplot[color=red,mark=*,
                         only marks,
                         point meta=x, % use x-value for nodes near coords
                         nodes near coords
                        ] coordinates {
                        (1.1,1)
                        (2.2,2)
                        (3.3,3)
                    };
                \addplot[] {x};

                \end{axis}
            \end{tikzpicture}

    %---------------------------------------------------------
    \end{document}

관련 정보