데이터 유형 열을 기반으로 산점에 색상을 지정하고 범례 항목을 만드는 방법

데이터 유형 열을 기반으로 산점에 색상을 지정하고 범례 항목을 만드는 방법

다음과 같은 파일이 있다고 가정해 보겠습니다.

\documentclass{standalone}
\usepackage{pgfplots,filecontents}
\begin{filecontents*}{data}
1 10 type1
2 25 type2
3 75 type2
4 100 type3
5 150 type3
\end{filecontents*}

\begin{document}  

\begin{tikzpicture}
    \begin{axis}   
    \addplot[scatter,only marks] 
        table[row sep=crcr] {data};         
    \end{axis}
\end{tikzpicture}

\end{document}

이는 다음 플롯을 생성합니다(코드가 세 번째 데이터 열을 도입하면서 장난감을 유모차 밖으로 던지고 (1,10) 및 (2,25) 좌표 플롯을 엉망으로 만든다는 점에 유의).

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

내가 어떻게 할:

  1. 각 분산점에 색상을 지정하거나 "유형" 열을 기준으로 각각 임의의 기호를 할당할 수도 있습니다. 나는 후자에 아마도 "if type=type1, Symbol=triangle" 유형 문이 필요할 것이라는 것을 알고 있습니다. 이는 괜찮습니다.
  2. 어떤 색상이 어떤 데이터 유형에 속하는지 나타내는 범례를 만듭니다.

\addplot각 데이터 유형 뒤에 별도의 명령을 사용하여 \addlegendentry{type1}이 작업을 수동으로 수행할 수 있지만 단일 addplot?

답변1

PGFPLOTS 매뉴얼에는 원하는 작업을 수행하는 예제가 있습니다. 현재 107페이지에 있습니다.4.5.11 산점도.

\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=south east]
    \addplot[
        scatter,only marks,scatter src=explicit symbolic,
        scatter/classes={
            a={mark=square*,blue},
            b={mark=triangle*,red},
            c={mark=o,draw=black,fill=black}
        }
    ]
    table[x=x,y=y,meta=label]{
        x    y    label
        0.1  0.15 a
        0.45 0.27 c
        0.02 0.17 a
        0.06 0.1  a
        0.9  0.5  b
        0.5  0.3  c
        0.85 0.52 b
        0.12 0.05 a
        0.73 0.45 b
        0.53 0.25 c
        0.76 0.5  b
        0.55 0.32 c
    };
    \legend{Class 1,Class 2,Class 3}
\end{axis}
\end{tikzpicture}
\end{document}

관련 정보