Composición tipográfica de una leyenda para un gráfico en un diagrama TikZ

Composición tipográfica de una leyenda para un gráfico en un diagrama TikZ

El siguiente código me proporciona la pantalla que quiero. (Sé que el código podría haber sido mucho más eficiente con foreachlos comandos). Me gustaría un código más consistente para la leyenda. ¿Alguien tiene alguna sugerencia?

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

Respuesta1

Vea si la siguiente solución puede ayudarlo a dibujar su 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}

ingrese la descripción de la imagen aquí

nota:

  • Mediante el uso del paquete, pgfplotsel código de su imagen se reduce significativamente, se simplifica y es mucho más claro.
  • para los precios del café utilizo las coordenadas dadas en sumwe(ejemplo práctico mínimo), sin embargo, se sospecha que los precios reales son aproximadamente cuarenta veces más altos. por lo que debe verificar los datos en la tabla \mydatay luego corregirlos en consecuencia.
  • Si desea tener un diagrama más grande o más pequeño, simplemente cambie la opción widthde 80 mm al ancho deseado
  • para la clase de documento selecciono standalonela que es útil para dibujar imágenes. sin embargo, puedes reemplazarlo con {amsart}.
  • Para posicionar la imagen en la página, no utilice el enfoque que muestra en sumwe. mejor es encerrarlo \begin{center} ... tikzpicture ... \end{center}o incluso mejor en un ambiente flotantefigure

información relacionada