TikZ 다이어그램의 플롯에 대한 범례 조판

TikZ 다이어그램의 플롯에 대한 범례 조판

다음 코드는 내가 원하는 디스플레이를 제공합니다. (명령을 사용하면 코드가 훨씬 더 효율적일 수 있다는 것을 알고 있습니다 foreach.) 범례에 대해 좀 더 일관된 코드를 원합니다. 누구든지 어떤 제안이 있습니까?

\documentclass[10pt]{amsart}
\usepackage{mathtools}


\usepackage{tikz}


\begin{document}



\noindent \hspace*{\fill}
\begin{tikzpicture}

%Two scatter plots are drawn.
%
%The axes are drawn.
\draw (0,0) -- (7,0);
\draw (0,0) -- (0,4.5);
%
%Tick marks on the x-axis is drawn.
\draw ($(0.5,0) +(0,2pt)$) -- ($(0.5,0) +(0,-2pt)$);
\draw ($(1.25,0) +(0,2pt)$) -- ($(1.25,0) +(0,-2pt)$);
\draw ($(2,0) +(0,2pt)$) -- ($(2,0) +(0,-2pt)$);
\draw ($(2.75,0) +(0,2pt)$) -- ($(2.75,0) +(0,-2pt)$);
\draw ($(3.5,0) +(0,2pt)$) -- ($(3.5,0) +(0,-2pt)$);
\draw ($(4.25,0) +(0,2pt)$) -- ($(4.25,0) +(0,-2pt)$);
\draw ($(5,0) +(0,2pt)$) -- ($(5,0) +(0,-2pt)$);
\draw ($(5.75,0) +(0,2pt)$) -- ($(5.75,0) +(0,-2pt)$);
\draw ($(6.5,0) +(0,2pt)$) -- ($(6.5,0) +(0,-2pt)$);
%
%Tick marks on the y-axis is drawn.
\draw ($(0,0) +(-2pt,0)$) -- ($(0,0) +(0,0)$);
\draw ($(0,0.5625) +(-2pt,0)$) -- ($(0,0.5625) +(2pt,0)$);
\draw ($(0,1.125) +(-2pt,0)$) -- ($(0,1.125) +(2pt,0)$);
\draw ($(0,1.6875) +(-2pt,0)$) -- ($(0,1.6875) +(2pt,0)$);
\draw ($(0,2.25) +(-2pt,0)$) -- ($(0,2.25) +(2pt,0)$);
\draw ($(0,2.8125) +(-2pt,0)$) -- ($(0,2.8125) +(2pt,0)$);
\draw ($(0,3.375) +(-2pt,0)$) -- ($(0,3.375) +(2pt,0)$);
\draw ($(0,3.9375) +(-2pt,0)$) -- ($(0,3.9375) +(2pt,0)$);
\draw ($(0,4.5) +(-2pt,0)$) -- ($(0,4.5) +(2pt,0)$);
%
%Horizontal lines are drawn in the first quadrant of the Cartesian plane.
\draw (0,0.5625) -- (7,0.5625);
\draw (0,1.125) -- (7,1.125);
\draw (0,1.6875) -- (7,1.6875);
\draw (0,2.25) -- (7,2.25);
\draw (0,2.8125) -- (7,2.8125);
\draw (0,3.375) -- (7,3.375);
\draw (0,3.9375) -- (7,3.9375);
\draw (0,4.5) -- (7,4.5);
%
%The label for the x-axis is typeset.
\node[anchor=north, inner sep=0] at ($(3.5,0) +(0,-0.65)$){Year};
%The label for the y-axis is typeset.
\node[anchor=north, inner sep=0, rotate=90] at ($(0,2.25) +(-1.25,0)$){Amount (US cents per lb)};
%
%The title for the plots is typeset.
\node[anchor=south, inner sep=0, align=center] at ($(3.5,0) +(0,4.75)$){Regular Coffee Profits\\Compared to Fair Trade Coffee\\Profits in Tanzania};
%
%Some tick marks along the x-axis are labeled.
\node[anchor=north, inner sep=0, font=\footnotesize] at ($(0.5,0) +(0,-0.15)$){2000};
\node[anchor=north, inner sep=0, font=\footnotesize] at ($(2,0) +(0,-0.15)$){2002};
\node[anchor=north, inner sep=0, font=\footnotesize] at ($(3.5,0) +(0,-0.15)$){2004};
\node[anchor=north, inner sep=0, font=\footnotesize] at ($(5,0) +(0,-0.15)$){2006};
\node[anchor=north, inner sep=0, font=\footnotesize] at ($(6.5,0) +(0,-0.15)$){2008};
%Some tick marks along the y-axis are labeled.
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,0) +(-0.15,0)$){0};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,0.5625) +(-0.15,0)$){20};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,1.125) +(-0.15,0)$){40};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,1.6875) +(-0.15,0)$){60};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,2.25) +(-0.15,0)$){80};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,2.8125) +(-0.15,0)$){100};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,3.375) +(-0.15,0)$){120};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,3.9375) +(-0.15,0)$){140};
\node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,4.5) +(-0.15,0)$){160};
%
%
%One of the two plots is drawn.
\draw ($(0.5,0) +(0,1.51875)$) -- ($(1.25,0) +(0,0.95625)$) -- ($(2,0) +(0,0.675)$) -- ($(2.75,0) +(0,0.675)$) -- ($(3.5,0) +(0,0.703125)$)
-- ($(4.25,0) +(0,1.18125)$) -- ($(5,0) +(0,1.153125)$) -- ($(5.75,0) +(0,1.659375)$) -- ($(6.5,0) +(0,1.74375)$);
\draw[fill] ($(0.5,0) +(0,1.51875)$) circle (1.5pt);
\draw[fill] ($(1.25,0) +(0,0.95625)$) circle (1.5pt);
\draw[fill] ($(2,0) +(0,0.675)$) circle (1.5pt);
\draw[fill] ($(2.75,0) +(0,0.675)$) circle (1.5pt);
\draw[fill] ($(3.5,0) +(0,0.703125)$) circle (1.5pt);
\draw[fill] ($(4.25,0) +(0,1.18125)$) circle (1.5pt);
\draw[fill] ($(5,0) +(0,1.153125)$) circle (1.5pt);
\draw[fill] ($(5.75,0) +(0,1.659375)$) circle (1.5pt);
\draw[fill] ($(6.5,0) +(0,1.74375)$) circle (1.5pt);
%
%The other of the two plots is drawn.
\draw[dashed] ($(0.5,0) +(0,3.628125)$) -- ($(1.25,0) +(0,3.65625)$) -- ($(2,0) +(0,3.65625)$) -- ($(2.75,0) +(0,3.65625)$)
-- ($(3.5,0) +(0,3.65625)$) -- ($(4.25,0) +(0,3.65625)$) -- ($(5.75,0) +(0,3.65625)$) -- ($(6.5,0) +(0,3.853125)$);
\draw[fill=white, thick] ($(0.5,0) +(0,3.628125)$) circle (1.5pt);
\draw[fill=white, thick] ($(1.25,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(2,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(2.75,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(3.5,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(4.25,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(5,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(5.75,0) +(0,3.65625)$) circle (1.5pt);
\draw[fill=white, thick] ($(6.5,0) +(0,3.853125)$) circle (1.5pt);
%
%
%The legend to the plots is typeset.
\draw (1.75,-1.25) -- (5.25,-1.25) --(5.25,-2.125) -- (1.75,-2.125) -- cycle;
\draw[dashed] ($(1.75,-1.5) +(4pt,0)$) -- ($(1.75,-1.5) +(32pt,0)$);
\draw [fill=white, thick] ($(1.75,-1.5) +(18pt,0)$) circle (1.5pt);
\node[anchor=west, inner sep=0, font=\footnotesize] at ($(1.75,-1.5) +(36pt,0)$){fair trade coffee}; 
\draw ($(1.75,-1.875) +(4pt,0)$) -- ($(1.75,-1.875) +(32pt,0)$);
\draw[fill]  ($(1.75,-1.875) +(18pt,0)$) circle (1.5pt);
\node[anchor=west, inner sep=0, font=\footnotesize] at ($(1.75,-1.875) +(36pt,0)$){regular coffee}; 

\end{tikzpicture}
\hspace{\fill}




\end{document}

답변1

다음 솔루션이 다이어그램을 그리는 데 도움이 될 수 있는지 확인하십시오.

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}
    \pgfplotstableread{% here should be real coffee price
                       % y1: regular coffee
                       % y2: fair trade coffee
        date    y1      y2
        2001    1.519   3.628
        2002    0.956   3.656
        2003    0.675   3.656
        2004    0.675   3.656
        2005    0.703   3.656
        2006    1.181   3.656
        2007    1.153   3.656
        2008    1.659   3.656
        2009    1.743   3.853
                    }\mydata
\begin{axis}[width=80mm,
grid,
tick label style = {font=\footnotesize,
                    /pgf/number format/1000 sep={}},
xlabel = year,
ylabel = {Amount (US cents per lb)},
 title = {Profits\\ Compared to Fair Trade Coffee\\ Profits in Tanzania},
title style={at={(0.5,1)},
             text width=0.8\linewidth,
             align=center},
legend style={at={(0.5,-0.16)},anchor=north},
legend cell align=left
            ]
% regular coffee
\addplot table[x=date, y=y1] {\mydata};
% fair trade coffee
\addplot table[x=date, y=y2] {\mydata};
%The legend
    \legend{regular coffee, fair trade coffee};
\end{axis}
    \end{tikzpicture}
\end{document}

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

메모:

  • 패킷을 사용하면 pgfplots이미지의 코드가 크게 줄어들고 단순화되며 훨씬 더 명확해집니다.
  • 커피 가격은 내가 지정한 좌표를 사용합니다.(최소 작업 예) 그러나 실제 가격은 약 40배 더 높은 것으로 의심됩니다. 따라서 테이블의 데이터를 확인한 \mydata다음 그에 따라 수정해야 합니다.
  • 더 크거나 작은 다이어그램을 갖고 싶다면 옵션을 width80mm에서 원하는 너비로 변경하세요.
  • standalone문서 클래스의 경우 이미지 그리기에 편리한 것을 선택합니다 . 그러나 로 바꿀 수 있습니다 {amsart}.
  • 페이지에서 이미지 위치를 지정하려면 다음과 같이 접근 방식을 사용하지 마십시오.. 더 나은 것은 그것을 \begin{center} ... tikzpicture ... \end{center}float 환경 에 포함하거나 더 나은 환경으로 묶는 것입니다.figure

관련 정보