4

I have to plot multiple lines in the same figure, using different markers to discriminate them. All these lines start from point (0,1) and end in (1,0) that are also the limit of the axes.

I'd prefer to do not draw the first and last markers, that are the ones in (1,0) and (0,1), but I don't know how to do it without tricks such as use a separated line for the markers only.

In addition, is it possible to add markers in the straight line connecting two coordinates? For example, I'd like to put extra markers in the line connecting the coordinates (0,1) and (0.4,0.6) without have to indicate the coordinates.

Here's a minimal example.

\documentclass{article}
\usepackage{pgf,pgfsys,pgffor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=1, ymin=0, ymax=1]
        \addplot coordinates {(0,1) (0.2,0.7) (0.5,0.5) (0.7,0.2) (1,0)};
        \addplot coordinates {(0,1) (0.4,0.6) (0.5,0.5) (0.6,0.4) (1,0)};
\end{axis}
\end{tikzpicture}

\end{document}

Here's a more complex example. I have saved on different files the coordinates of each lines (here included with filecontents).

\documentclass{article}
\usepackage{pgf,pgfsys,pgffor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}
\usepackage{filecontents}


\begin{filecontents}{line1.dat}
  0.0000000e+000  1.0000000e+000
  6.8496983e-001  9.2401472e-001
  7.0716535e-001  9.1958307e-001
  7.2173728e-001  9.1351472e-001
  7.3036908e-001  9.0720588e-001
  7.4013221e-001  9.0099625e-001
  7.4786697e-001  8.9202456e-001
  7.5596517e-001  8.8392894e-001
  7.6269674e-001  8.7239867e-001
  7.7155769e-001  8.5579528e-001
  7.8143617e-001  8.2917002e-001
  8.3316668e-001  7.8038268e-001
  8.5914322e-001  7.7066400e-001
  8.7527488e-001  7.6191438e-001
  8.8414461e-001  7.5456052e-001
  8.9422721e-001  7.4724438e-001
  9.0125715e-001  7.3818180e-001
  9.0871819e-001  7.2991961e-001
  9.1381216e-001  7.1925515e-001
  9.1990775e-001  7.0431023e-001
  1.0000000e+000  0.0000000e+000
\end{filecontents}
\begin{filecontents}{line2.dat}
  0.0000000e+000  1.0000000e+000
  1.2500000e-001  3.2500000e-001
  2.7083333e-001  2.7083333e-001
  3.2500000e-001  1.2500000e-001
  1.0000000e+000  0.0000000e+000
\end{filecontents}
\begin{document}

\begin{tikzpicture}
    \pgfplotstableread{line1.dat}\lineA
    \pgfplotstableread{line2.dat}\lineB
\begin{axis}[xmin=0, xmax=1, ymin=0, ymax=1]
        \addplot[red,mark=o, mark repeat={5}] table \lineA;
        \addplot[cyan,mark=square] table \lineB;
\end{axis}
\end{tikzpicture}

\end{document}

1 Answer 1

5

You can connect the plot to the corners by adding -- (axis cs:1,0) (axis cs:0,1) -- (current plot begin) at the end of the \addplot command. This expression can also be wrapped in a style to make its use easier:

\tikzset{
    connect to corners/.style={
        insert path={-- (axis cs:1,0) (axis cs:0,1) -- (current plot begin)}
    }
}

For your second request, you can use a mathematical expression instead of a coordinate list:

\addplot [red, mark=*, samples at={0.4,0.6}] {1-x} [connect to corners];

Here's the complete code:

\documentclass{article}
\usepackage{pgf,pgfsys,pgffor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}

\begin{document}

\tikzset{
    connect to corners/.style={
        insert path={-- (axis cs:1,0) (axis cs:0,1) -- (current plot begin)}
    }
}

\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=1, ymin=0, ymax=1]
        \addplot coordinates {(0.2,0.7) (0.5,0.5) (0.7,0.2)} [connect to corners];
        \addplot [red, mark=*, samples at={0.4,0.6}] {1-x} [connect to corners];
\end{axis}
\end{tikzpicture}

\end{document}
5
  • Thanks @Jake! One more thing please, what if I want to put extra markers in the line connecting the coordinates (0,1) and (0.2,0.7)? I think I can change {1-x}, but maybe there's a easier way to do it. Actually, my initial coordinate ''(0.2,0.7)'' are in a file and I'd rather not to have to read them and define the appropriate function that substitute {1-x}.
    – Nicola
    May 29, 2013 at 14:05
  • @Nicola: I think the best way to do that depends on where the data you're plotting comes from. Will you need arbitrary straight lines that start and end somewhere in the unit square, or do they all start at (0,1)? Do you know the coordinates, or the angles and lengths of the lines? Are the plot marks placed at certain lengths along the line, or at certain x coordinates? What do the lines represent?
    – Jake
    May 29, 2013 at 14:11
  • The lines are contour lines calculated with an external program (MATLAB) and the coordinates are saved on different files, one for each line. They all start in (0,1) and end in (1,0), but I don't have a priori information on the angles or length of the first/last segment (the one from (0,1) and the next coordinate). I'd rather to put one or two marks in this segment that otherwise are remains with no mark. Of course, I could read the second coordinate and define an appropriate function to replace {1-x} but this will take time. Is there an easy way to append the segment with few marks?
    – Nicola
    May 29, 2013 at 14:22
  • @Nicola: So all those contour lines start at (0,1), go to some other coordinate in a straight line segment, and then go to (1,0) in another straight line segment? And you want to place plot markers along those straight line segments? Where do you want them? At equal intervals along the line segments?
    – Jake
    May 29, 2013 at 14:26
  • Yes, all these contour lines start at (0,1), go to an other coordinate in a straight line, then there is a curve defined with other coordinates, and then another straight line to end in (1,0). I have added a more complex example. Since the straight lines seems to empty to me, I'd like to put some marks on them, for example one in the middle of the straight line, or 2 marks at equal intervals.
    – Nicola
    May 29, 2013 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .