Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to plot a figure with TikZ. I have a few .txt files with the coordinates of the curves that I want to plot, in a list such as

file2.txt, 
file4.txt,
...
file30.txt

and I want to add them with \plot file in a axis environment. Also, I'd like to put them with different colors, creating a shading effects, let's say from blue to red.

In short, I want to create a sort of contour plot, but since I don't have the 3D function I have to import the coordinates.

I wrote this code, where I use two variables in \foreach, one for the file name, \x, and one for the shading of the color, \y, (see lines 'Original Idea'). It doesn't work because of some kind of trouble between \foreach and the axis environment.

I thought about other options, such as to explicate a relation between \x and \y (see code lines), use \pgfplotsinvokeforeach or an other trick with \edef but they doesn't work for different reasons:

  • if I explicit the relation it seem like it doesn't want to calculate 3*\x (undefined control sequence)

  • with \pgfplotsinvokeforeach it says 'illegal unit of measure'

  • same for \edef

I read some topics on the use of \pgfplotsinvokeforeach and the trick with \edef but probably I haven't understood correctly how to use them in my code.

Can you give me a solution? Better it would be if you give me the corrections for all the 4 possible cases I proposed :-)

Here's the code

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

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{./figure/regionDep0/orthogonal}
    \begin{axis}[
        xmin=0, xmax=1, ymin=0, ymax=1, width=8cm, height=8cm]
        % Original idea: not working
        \foreach \x/\y in {2/0,4/7,...,30/98}
        {
            \plot[color=blue!\y!red] file {\filename \x.txt};
        }
%   
%       % substituting \y as function of \x: not working
%       \foreach \x in {2,4,...,30}
%       {
%           \plot[color=blue!3*\x!red] file {\filename \x.txt};
%       }
%       
%       % Trying with \pgfplotsinvokeforeach: not working
%       \pgfplotsinvokeforeach{2,4,...,30}
%       {
%       \plot [color=blue!3*#1!red] file {\filename #1.txt};
%       }
%
%       %   Trying with \edef\temp{...}: not working
%       \foreach \x/\y in {2/0,4/7,...,30/98}
%       {
%           \edef\temp{\noexpand\plot [color=blue!\y!red] file {\filename \x.txt};}
%           \temp
%       }       
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
share|improve this question
    
Welcome to TeX.SX. Note that you don't have to sign with your name since it automatically appears in the lower right corner of your post. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Claudio Fiandrino Mar 14 '13 at 11:19
add comment

1 Answer

up vote 6 down vote accepted

If you're using an axis environment, which is provided by the PGFPlots package, you should use \addplot instead of \plot.

To use a different colour for each plot, one option might be using the mesh plot type together with point meta=explicit and meta expr=\i. This allows the use of colormaps instead of being limited to colorA!<value>!colorB expressions:

\documentclass{article}
\usepackage{pgfplots}  % Loads all required packages automatically
\usepackage{filecontents}
\begin{filecontents*}{file1.txt}
0 0
1 1
2 2
3 1
4 0
\end{filecontents*}
\begin{filecontents*}{file2.txt}
0 1
1 3
2 5
3 3
4 1
\end{filecontents*}
\begin{filecontents*}{file3.txt}
0 2
1 5
2 7
3 5
4 2
\end{filecontents*}

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{file}
    \begin{axis}[
            colormap/bluered
        ]
        \foreach \i in {1,2,3}
        {
            \addplot +[thick, no markers, mesh, point meta=explicit] table [meta expr=\i] {\filename \i.txt};
        }   
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

Alternatively (and probably better), you can use the contour prepared plot style if you put all contours into a single file. That way, you can even get labels on the contours (the labels can be adjusted or switched off, of course):

\documentclass{article}
\usepackage{pgfplots}  % Loads all required packages automatically
\pgfkeys{/pgf/number format/relative round mode=fixed}
\usepackage{filecontents}
\begin{filecontents*}{contours.txt}
0 0 0
1 1 0
2 2 0
3 1 0
4 0 0

0 1 1
1 3 1
2 5 1
3 3 1
4 1 1

0 2 2
1 5 2
2 7 2
3 5 2
4 2 2
\end{filecontents*}

\begin{document}

\begin{figure}
    \begin{tikzpicture} 
    \newcommand{\filename}{file}
    \begin{axis}
    \addplot [contour prepared] table {contours.txt};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
share|improve this answer
    
Probably you miss the point. The problem is to use two indexes to specify the file and the color, or one index to set both. If you use even \plot instead of \addplot without caring about the color it works. But with your version \addplot +[no markers] file {\filename \i.txt}; the colours are set randomly without creating the shading. –  Nicola Mar 14 '13 at 14:12
    
@Nicola: Ah, yeah, sorry, I thought your main problem was getting the thing to run in principle since you were using the wrong syntax. I've edited my answer to show two different ways of achieving what I think you're trying to do –  Jake Mar 14 '13 at 14:31
    
Thanks for the trick Jake. I still have a problem with your solution. If I plot more than 5 curves, some of them get dashed. Unfortunately I cannot post the whole code, I edit your code adding more \begin{filecontents*} ... \end{filecontents*} and I have the same problem, meaning that it is not a problem of the files. Do you have any ideas? –  Nicola Mar 14 '13 at 15:32
    
@Nicola: Yes, just remove the + before the \addplot (that way, the plot style cycle is not used). –  Jake Mar 14 '13 at 15:58
    
Thanks so much, now it works! –  Nicola Mar 14 '13 at 16:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.