为 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图像的代码将显著减少、简化和更加清晰
  • 对于咖啡价格,我使用给定的坐标姆韦(最小工作示例),然而怀疑实际价格可能高出约四十倍。因此您需要检查表中的数据\mydata,然后相应地进行更正。
  • 如果您想要更大或更小的图表,只需将选项width从 80mm 更改为所需宽度即可
  • 对于文档类,我选择standalone便于绘制图像的类。但是,您可以用 替换它{amsart}
  • 对于页面上图像的定位,不要使用您在姆韦. 最好是将它包含\begin{center} ... tikzpicture ... \end{center}在浮动环境中figure

相关内容