為 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

相關內容