tikzlibrary datavisualization을 사용하여 x축을 텍스트 레이블로 x 데이터가 있는 서수 축으로 바꾸는 방법은 무엇입니까?

tikzlibrary datavisualization을 사용하여 x축을 텍스트 레이블로 x 데이터가 있는 서수 축으로 바꾸는 방법은 무엇입니까?

배경

의 데이터 섹션에 이와 같은 데이터가 있다고 가정합니다 datavisualization. 이 데이터는 파일에서도 나올 수 있습니다.

        date,birds      
        30/10/2023,1200
        02/10/2023,1229
...

또는

        species,birds       
        spec4,1200
        spec1,1229
...

\datavisualization두 가지 시각화를 제공하며 scientific axesschool book axes다 xy 데이터가 필요합니다.

질문

간단한 순서 척도를 도입하기 위해 텍스트 x-데이터를 카운터로 대체하는 것은 큰 문제가 아닙니다. 그러나 원본 x 데이터를 보존하거나 액세스하고 이를 1,2,... 대신 x축에 레이블로 배치하는 방법은 무엇입니까?

시도

아래 코드는 \pgfdeclaredataformat{ordinalX}x-data를 간단한 카운터 NX로 대체하는 를 제공합니다.

알아요, 나는~할 수 있었다ticks={major at={1 as spec4,...}}x축 명령문에 직접 via 등의 목록을 작성합니다 . 하지만 이미 데이터 섹션에 나와 있는 데이터를 처리할 때 이는 좋은 아이디어가 아닙니다.

여기에 사용된 데이터 범위, 액세스, 데이터 구조에 대해 전혀 모르는 것 같습니다. 또는 pgfmanual 단독 wrt에서 올바른 pgfkey를 추측하는 것조차 모르는 것 같습니다 datavisualization.

pgfkeys, l3, "array", data-object 등과 같은 전역 변수를 도입해야만 이 문제를 해결할 수 있습니까? 그렇다면 선언된 형식이 추출된 원본 x-데이터를 거기에 배치합니까?

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{datavisualization}

\newcounter{NX}

% ~~~ introducing ordinal values for x ~~~~~~~~~
%     converts (date,birds) into (NX,birds) in (x,y) world
\pgfdeclaredataformat{ordinalX}
    {}
    {}
    {#1,#2}
    {   % ~~~ new x-values: one-by-one ~~~~~~~~~
        \pgfmathaddtocounter{NX}{1}
        \pgfkeyssetvalue{/data point/x}{\theNX}     
        % ~~~ preserving the y-data ~~~~~~~~~~~~
        \pgfkeyssetvalue{/data point/y}{#2}         
        % ~~~ original x-data, to be treated as labels ~~~~
        \pgfkeyssetvalue{/data point/mylabel}{#1}       
        % ~~~ storing NX, y and (?) mylabel ~~~~~
        \pgfdatapoint
    }
    {}{}

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
 \begin{tikzpicture}
    \datavisualization [
        scientific axes=clean,
        x axis={label=X,
                ticks={label=mylabel} % can't put date as labels
        },      % rather it likes a list ?from data section?
        y axis={label=birds},
        all axes=grid,      
    ][  
        visualize as line,
    ] data [format=ordinalX] {
%       date,birds              % skipping header
        30/10/2023,1200
        02/10/2023,1229
        06/11/2023,980
        09/11/2023,570
        13/11/2023,308
        16/11/2023,2590
        20/11/2023,11700
    }
    %     TEST / Bypass ?
    % ~~~ if I had access to, say, /data point/mylabel here ...
    %     I might be able to iterate and place date as labels
    info{
        \foreach \x in {1,2,...,7}
            \node[rotate=90,anchor=east,teal] 
                at (visualization cs: x={\x},y={-2e3}) {X-label};
    }
    ;
 \end{tikzpicture}
\end{document}

결과

답변1

나는 사용하고 적응하는 솔루션을 찾았습니다2013년 Jake의 솔루션. 키 포인트:

  • 대부분의 일은 성명서에서 일어납니다.x axis={..., ticks={...
  • 그것은typesetter
  • styles the nodes라벨용(회전 등 )
  • 조판기는 global TeX list배열처럼 기능하는 a에 의존합니다.
  • 선언된 형식 내에서 ordinalX해당 appends목록에
  • 나는 LaTeX counter대신에 a를 사용합니다 predefined TeX \@tempcnta.StackExchange에서 여기를 참조하세요.
  • \makeatletter포함되어 있지만 \makeatother효과가 없는 것 같습니다. 아마도 사용된 카운터가 다르기 때문일 것입니다.
\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{datavisualization}

\newcounter{NX} % <<< using LaTeX counter istead of \@tempcnta

\makeatletter
    % ~~~ introducing ordinal values for x ~~~~~~~~~
    %     converts (date,birds) into (NX,birds) in (x,y) world
    \pgfdeclaredataformat{ordinalX}%
        {} % No catcode changes
        {\xdef\label@list{}}%                       initialise a global label list
        {#1,#2}%                                    data format
        {%
            \pgfmathaddtocounter{NX}{1}%            advance
            \pgfkeyssetvalue{/data point/x}{\theNX}%store counters result as x-data 
            \pgfkeyssetvalue{/data point/y}{#2}%    the y-data (birds)          
            \xdef\label@list{\label@list,"#1"}%     append label to list
            \pgfdatapoint
        }%
        {}{}

    % ~~~ typesetting the x-axis: putting labels instead of 1,2,3, ... ~~~~~~~~
    \def\ticksfromlabellist#1{%
        \pgfmathparse{{\label@list}[#1]}%   retrieve label one by one   
        \pgfmathresult%                     result: text, i.e. 30/10/2023
    }    
\makeatother


% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
 \begin{tikzpicture}
    \datavisualization [
        scientific axes=clean,
        x axis={
            label=Date,
            ticks={                                             % NEW
                step=1, 
                tick typesetter/.code=\ticksfromlabellist{##1}, % typesetting labels, replacing 1,2,3,...
%               style={text height=1ex},                        % for proper vertical alignment
                node style={rotate=90,anchor=east,teal},        % rotating etc.
            } 
        },  
        y axis={label=birds},
        all axes=grid,      
    ][  
        visualize as line,
    ] data [format=ordinalX] {
%       date,birds              % skipping header
        30/10/2023,1200
        02/10/2023,1229
        06/11/2023,980
        09/11/2023,570
        13/11/2023,308
        16/11/2023,2590
        20/11/2023,11700
    };
 \end{tikzpicture}
\end{document}

결과

관련 정보