Composição de uma legenda para um gráfico em um diagrama TikZ

Composição de uma legenda para um gráfico em um diagrama TikZ

O código a seguir me dá a exibição que desejo. (Sei que o código poderia ter sido muito mais eficiente com foreachcomandos.) Gostaria de um código mais consistente para a legenda. Alguém tem alguma sugestão?

\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}

Responder1

veja se a solução a seguir pode ajudá-lo no desenho do seu diagrama:

\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}

insira a descrição da imagem aqui

observação:

  • com o uso do pacote, pgfplotso código da sua imagem é significativamente reduzido, simplificado e muito mais claro
  • para preços de café, uso as coordenadas fornecidas em seueu(exemplo prático mínimo), no entanto, suspeita-se que os preços reais sejam cerca de quarenta vezes mais elevados. então você precisa verificar os dados na tabela \mydatae corrigi-los adequadamente.
  • se quiser um diagrama maior ou menor, basta alterar a opção widthde 80mm para a largura desejada
  • para a classe de documento eu seleciono standaloneo que é útil para desenhar imagens. no entanto, você pode substituí-lo por {amsart}.
  • para posicionamento de imagem na página não use abordagem como você mostra em seueu. melhor é encerrá-lo \begin{center} ... tikzpicture ... \end{center}ou melhor ainda em ambiente flutuantefigure

informação relacionada