1

My code is below.

\documentclass[11pt]{article} \usepackage{tikz} \usetikzlibrary{patterns,arrows,decorations.pathreplacing,calc,intersections,through,backgrounds,plotmarks} \usepackage{float} \begin{document} \begin{figure}[H] \centering \begin{tikzpicture}[xscale=3,yscale=3] \draw [<->] (0,3)--(0,0)--(3,0); \node[mark size=7pt,color=black] at (0.5,0.0931) {\pgfuseplotmark{diamond}}; \node[mark size=7pt,color=black] at (1.5,0.2401) {\pgfuseplotmark{diamond}}; \node[mark size=7pt,color=black] at (2.5,0.7857) {\pgfuseplotmark{diamond}}; \node[mark size=4pt,color=black] at (0.5,0.1310) {\pgfuseplotmark{square}}; \node[mark size=4pt,color=black] at (1.5,0.4881) {\pgfuseplotmark{square}}; \node[mark size=4pt,color=black] at (2.5,0.3571) {\pgfuseplotmark{square}}; \node [below] at (0.5,0) {text 1}; \node [below] at (1.5,0) {text 2}; \node [below] at (2.5,0) {text 3}; \node [left] at (0,3) {text}; \end{tikzpicture} \end{figure} \end{document} 

I would like to add a legend to it. My search suggests pgfplots, but the example is drawing a continuous function rather than discrete points in my case, and different curves are differentiated by color.

3 Answers 3

3

If you don't want to switch to pgfplots, you would need to add it manually which is possible but not very elegant:

\documentclass[border=10pt]{standalone} \usepackage{tikz} \usetikzlibrary{ plotmarks, positioning, fit } \begin{document} \begin{tikzpicture}[xscale=3, yscale=3] \draw [<->] (0,3) -- (0,0) -- (3,0); \node[mark size=7pt, color=black] at (0.5,0.0931) {\pgfuseplotmark{diamond}}; \node[mark size=7pt, color=black] at (1.5,0.2401) {\pgfuseplotmark{diamond}}; \node[mark size=7pt, color=black] at (2.5,0.7857) {\pgfuseplotmark{diamond}}; \node[mark size=4pt, color=black] at (0.5,0.1310) {\pgfuseplotmark{square}}; \node[mark size=4pt, color=black] at (1.5,0.4881) {\pgfuseplotmark{square}}; \node[mark size=4pt, color=black] at (2.5,0.3571) {\pgfuseplotmark{square}}; \node[below] at (0.5,0) {text 1}; \node[below] at (1.5,0) {text 2}; \node[below] at (2.5,0) {text 3}; \node[left] at (0,3) {text}; \node[anchor=north east, minimum width=5em, align=left] at (3,3) (legendA) {label 1}; \node[left=5pt of legendA, mark size=7pt, color=black] (labelA) {\pgfuseplotmark{diamond}}; \node[below=10pt of legendA, minimum width=5em, align=left] (legendB) {label 2}; \node[left=5pt of legendB, mark size=4pt, color=black] (labelB) {\pgfuseplotmark{square}}; \node[fit={(legendA) (labelA) (legendB) (labelB)}, draw] {}; \end{tikzpicture} \end{document} 

output of above code

1
  • 2
    One could put the marks and label texts in a \foreach command maybe and have the output render a bit more automatically ... Commented Nov 14, 2024 at 14:23
2

pgfplots would be the more developed approach: look for symbolic coordinates in its manual.

Below I rearranged (refactored) your code step by step to show you one way to do it with plain tikz. Most parts should be understandable from the initial comment section. E.g. by introducing styles you both simplify your code AND can have consistent changes by just editing the self-defined style-options.

Please don't puzzle the commands \dmnd with their tikz-options dmnd: they are different things in their contexts. (You may want to use more sophisticated names.)

A speciality remains putting those diamond and square symbol nicely in the legend.

The legend I'd put as one node, with dedicated options, like line breaks (align= and \\). // The nodes text can be any valid LaTeX statement. Turned out both symbols should be raised a little, followed perhaps by some horizontal space.

If you'd use the (commented out) saveboxes, the symbols will be larger. However, both approaches still can have some vertical and horizontal find tuning from a typesetting point of view.

 lgnd/.style={anchor=east,draw,align=left,inner sep=8pt}, ... \node[lgnd,yshift=38mm] at (X) { \raisebox{4.5pt}{\dmnd}\hspace{.7em}: Diamonds are \dots\\ \raisebox{4.5pt}{\sqrr}\hspace{.8em}: Other data }; 

result

% ~~~ REFACTORING ~~~~~~~~~~~~~~~~~~~ % % 1. some structured editing (indenting, comments etc.) % 2. keeping only libraries relevant here; changing arrow tips % 3. introducing shortcuts for long statements % 4. style shortcuts for diamond and square % 5. suggestion for legend // left trial with saveboxes as demo \documentclass[11pt]{article} \usepackage{tikz} \usetikzlibrary{ % patterns, arrows.meta, % <<< % decorations.pathreplacing, % calc, % intersections, % through, % backgrounds, plotmarks } \usepackage{float} % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \newcommand\dmnd[0]{\pgfuseplotmark{diamond}} \newcommand\sqrr[0]{\pgfuseplotmark{square}} % ~~~ saveboxes ~~~~~~~~~~~~~ %\newsavebox\dbx %\newsavebox\sbx % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \begin{document} \begin{figure}[H] \centering \begin{tikzpicture}[ xscale=3,yscale=3, >={Stealth}, dmnd/.style={mark size=7pt,color=black}, sqrr/.style={mark size=4pt,color=magenta}, lgnd/.style={anchor=east,draw,align=left,inner sep=8pt}, ] % ~~~ axes ~~~~~~~~~~~~~~~~~~~~~~~~~~~ \draw [<->] (0,1.5) coordinate(Y) --(0,0) --(3,0) coordinate(X); % <<< better? % ~~~ data ~~~~~~~~~~~~~~~~~~~~~~~~~~ \node[dmnd] at (0.5,0.0931) {\dmnd}; \node[dmnd] at (1.5,0.2401) {\dmnd}; \node[dmnd] at (2.5,0.7857) {\dmnd}; \node[sqrr] at (0.5,0.1310) {\sqrr}; \node[sqrr] at (1.5,0.4881) {\sqrr}; \node[sqrr] at (2.5,0.3571) {\sqrr}; % ~~~ labels ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \node [below] at (0.5,0) {text 1}; \node [below] at (1.5,0) {text 2}; \node [below] at (2.5,0) {text 3}; % \node [left] at (0,3) {text}; \node [anchor=east] at (Y) {text}; % try anchors instead % ~~~ legend (one way to do and place it) ~~~~~~~~~~ % \sbox{\dbx}{\dmnd} % \sbox{\sbx}{\sqrr} % \node[lgnd] at (2.5,1.2) { % \raisebox{4.5pt}{\usebox{\dbx}}\hspace{.7em}: Diamonds are \dots\\ % \raisebox{4.5pt}{\usebox{\sbx}}\hspace{.8em}: Other data % }; \node[lgnd,yshift=38mm] at (X) { \raisebox{4.5pt}{\dmnd}\hspace{.7em}: Diamonds are \dots\\ \raisebox{4.5pt}{\sqrr}\hspace{.8em}: Other data }; \end{tikzpicture} \end{figure} \end{document} 
2

Here is a way to do it with:

  • pgfplots
  • one graphic file AND one main document

1. pgfplots

Use documentclass standalone, which will simplify your work considerably. See further down below.

Basic simplified structure of environments for pgfplots, i.e. each axis can have one or more datasets displayed:

\begin{tikzpicture} \begin{axis}[...] \addplot ... \addplot ... \end{axis} \end{tikzpicture} 

Your 1st data set may be written like this:

  • old: your x/y coordinates
  • new: with symbolic coordinates
  • because these are used several times, I abbreviate them in the preamble
% ~~~ change you labels here ~~~~~~~~~~~~~~~ \newcommand\LA[0]{text 1} \newcommand\LB[0]{text 2} \newcommand\LC[0]{text 3} ... % ~~~ 1st set of data ~~~~~~~~~~~~~~~~~~~~~~ \addplot[ mark=diamond, % chose your symbol only marks, % i.e. omit lines ] coordinates { % (0.5,0.0931) (1.5,0.2401) (2.5,0.7857) % old (\LA,0.0931) (\LB,0.2401) (\LC,0.7857) % new }; \addlegendentry{Your diamonds} 

Kindly see all the comments in the code. You may want to see the mass-spectrometer-example following the same approach for symbolic x coords with some different focus.


pgfplots

\documentclass[10pt,border=3mm,tikz]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=newest} % ~~~ change you labels here ~~~~~~~~~~~~~~~ \newcommand\LA[0]{text 1} \newcommand\LB[0]{text 2} \newcommand\LC[0]{text 3} % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \begin{document} \begin{tikzpicture} % ~~~ axis == plot frame ~~~~~~~~~~~~~~~~~~~ \begin{axis}[ title=Some title, ymin=0, ymax=1, % data related: xtick=data, % avoids multiple labels symbolic x coords={\LA,\LB,\LC}, % legend related: legend cell align=left, legend pos=north west, ] % ~~~ 1st set of data ~~~~~~~~~~~~~~~~~~~~~~ \addplot[ mark=diamond, % chose your symbol only marks, % i.e. omit lines ] coordinates { % (0.5,0.0931) (1.5,0.2401) (2.5,0.7857) % old (\LA,0.0931) (\LB,0.2401) (\LC,0.7857) % new }; \addlegendentry{Your diamonds} % ~~~ 2nd set of data ~~~~~~~~~~~~~~~~~~~~~~ \addplot[ mark=square, % chose your symbol only marks, magenta, % if you need color ] coordinates { (\LA,0.1310) (\LB,0.4881) (\LC,0.3571) % new }; \addlegendentry{Your squares} \end{axis} \end{tikzpicture} \end{document} 

2. Compile this file into a pdf

... to obtain e.g. tikzPgfplotsLegend.pdf (from tikzPgfplotsLegend.tex)

  • if you are done with the drawing, the pdf is all you'll need
  • when you still need to do changes, compile it again
  • i.e. you decoupled the graphic-compile from your main-compile ... which increases your degree of freedom considerably

3. Your main document

Edit and compile e.g. main.tex like so:

  • use whatever documentclass you prefer
  • \usepackage{graphicx} to include aboves .pdf
  • show it inline or in a floating figure-envirenment, to mention a few choices.

\documentclass[11pt]{article} \usepackage{graphicx} % to include the drawing as .pdf \usepackage{lipsum} % blind text \begin{document} \lipsum[4][1-3] \includegraphics[width=.5\textwidth]{tikzPgfplotsLegend} \ inlined\bigskip \lipsum[5] \begin{figure}[b] \centering \includegraphics[scale=.4]{tikzPgfplotsLegend} \caption{That's still a vector graphic --- just zoom in} \end{figure} \end{document} 

mainresult

3
  • 1
    Thank you very much! I have two follow-up questions. 1. What role does "\pgfplotsset{compat=newest}" play? 2. How did you choose the color? I did not see any codes specifying the red color. Commented Nov 15, 2024 at 9:25
  • Fine. // It makes sure you use the latest update, which may trigger your Latex installation to actually update this package. Look for magenta in the 2nd \addplot. See tikz.dev for both, vis the toggle button at the top. Commented Nov 15, 2024 at 9:32
  • Though you can't retrieve older package versions via CTAN, the update announcement are there. Just peek through the links found here ctan.org/ctan-ann/pkg/pgfplots . The latest version is here: ctan.org/pkg/pgfplots . Commented Nov 15, 2024 at 9:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.