[Tex/LaTex] label curve and/or contour name created with pgfplot via gnuplot

gnuplotlabelspgfplotstikz-pgf

I will want to name different contour as one can see it on topographic card. How to do that?
I want label on different curve with the parameter that distinguish the curves. I have created 10+10 curves by gnuplot inside tikz environnement.

All the code that give here it due to this internet site. So thank many. Someone can help me to finish my graph by put one little label (like -10°, -20°, …, 90°) on each curve?

code ici :

    \documentclass[a4paper]{article}
        \usepackage[T1]{fontenc}
        \usepackage[utf8,applemac]{inputenc}
        \usepackage{lmodern, textcomp}
        \usepackage{mathrsfs,bm}
        \usepackage{amsmath,amssymb,amscd}
        \usepackage{comment,relsize}
        \usepackage[frenchb]{babel}
        % ==============================================
        \usepackage[babel=true,kerning=true]{microtype}%pour le package tikZ et les deux points``:''
        %%% TikZ packages.
        \usepackage{pgf,tikz}
        \usepackage{pgfplots}
        %
        \pagestyle{empty}

        \pgfplotsset{compat=newest}
        % ==============================================
        \begin{document}
        % ==============================================

        \begin{tikzpicture}
        % réglage de la grille de coordonnées
        \begin{axis}
          [ grid=major,
            no markers,
            axis on top,
            tick label style={font=\small},
            %extra y tick style={grid=major},
            %extra x tick style={grid=major}
            minor y tick num=1,
            minor x tick num=1,
            smooth,
            xlabel={D{\'e}clinaison du miroir: $D_m$},
            ylabel={Distance z{\'e}nithale du miroir: $I_m$},
            xmin=0, xmax=180,
            ymin=-0, ymax=180,
            width=1\textwidth,
            height=1\textwidth,
            legend style={at={(0.02,0.97)},anchor=north west},
            legend pos=south east,
            legend cell align=left
           ]%
        % Création des graphes f(x,y)=0 via GNUPLOT
        \addplot +[ no markers, raw gnuplot, thick, empty line = jump ]%
        gnuplot {%
              set contour base;
              set cntrparam levels discrete 0.0000;
              unset surface;
              set view map;
              set grid;
              set isosamples 200;
                set xrange [0:180];
            set yrange [0:180];
            D=pi/180*165;           %<<--------- ICI
            I=pi/180*90;            %<<--------- ICI
        k1=tan(pi/180*10);
        f1(x,y)=k1*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k2=tan(pi/180*20);
        f2(x,y)=k2*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k3=tan(pi/180*30);
        f3(x,y)=k3*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k4=tan(pi/180*40);
        f4(x,y)=k4*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k5=tan(pi/180*50);
        f5(x,y)=k5*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k6=tan(pi/180*60);
        f6(x,y)=k6*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k7=tan(pi/180*70);
        f7(x,y)=k7*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k8=tan(pi/180*80);
        f8(x,y)=k8*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k9=tan(pi/180*90);
        f9(x,y)=k9*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
            %
            splot f1(x,y), f2(x,y), f3(x,y), f4(x,y), f5(x,y), f6(x,y), f7(x,y), f8(x,y), f9(x,y) ;
            %
            };%
        %
        \addplot +[no markers, raw gnuplot, thick, empty line = jump  ]%
        gnuplot {%
              set contour base;
              set cntrparam levels discrete 0.0000;
              unset surface;
              set view map;
              set grid;
              set isosamples 200;
                set xrange [0:180];
            set yrange [0:180];
            D=pi/180*165;           %<<--------- ICI
            I=pi/180*90;            %<<--------- ICI
        k0=tan(pi/180*0);
        f0(x,y)=k0*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k1=tan(pi/180*-10);
        g1(x,y)=k1*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k2=tan(pi/180*-20);
        g2(x,y)=k2*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k3=tan(pi/180*-30);
        g3(x,y)=k3*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k4=tan(pi/180*-40);
        g4(x,y)=k4*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k5=tan(pi/180*-50);
        g5(x,y)=k5*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k6=tan(pi/180*-60);
        g6(x,y)=k6*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k7=tan(pi/180*-70);
        g7(x,y)=k7*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k8=tan(pi/180*-80);
        g8(x,y)=k8*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
        k9=tan(pi/180*-90);
        g9(x,y)=k9*(cos(D)+cos((D-2*pi/180*x))*tan(pi/180*y)**2)-2*sin(I)*sin(pi/180*x)*tan(pi/180*y)-cos(I)*(sin(D)+sin((D-2*pi/180*x))*tan(pi/180*y)**2);
            %
            splot g1(x,y), g2(x,y), g3(x,y), g4(x,y), g5(x,y), g6(x,y), g7(x,y), g8(x,y), g9(x,y) ;
            splot f0(x,y);
          }; %
        % Ajout d'une légende pour identifier les familles de courbes.
        %Positionnement à adapter pour chaque nouvelle compilation = pas efficace dutout...
     \node at (axis cs:165,160) [pin={90:\relsize{-1}{$\alpha_0=10\degres$}},inner sep=0pt] {}; %  a modifier manuellement...
\node at (axis cs:50,57) [pin={170:\relsize{-1}{$\alpha_0=90\degres$}},inner sep=0pt] {};
\node at (axis cs:118,120) [pin={200:\relsize{-1}{$\alpha_0=90\degres$}},inner sep=0pt] {};
\node at (axis cs:155,90) [pin={75:\relsize{-1}{$\alpha_0=0\degres$}},inner sep=0pt] {};

 % Légende à modifier ici pour chaque couple (D ; I) du cadran à réflexion.
 % \addlegendimage{empty legend}\addlegendentry{\relsize{-1.5}{$\mathscr{C}_{f_{\alpha_0}}$, pour $\alpha_0=\left\{10\degres;\ldots; 90\degres\right\}$}}
 % \addlegendimage{empty legend}\addlegendentry{\relsize{-1.5}{$\mathscr{C}_{f_{\alpha_0}}$, pour $\alpha_0=\left\{-90\degres;\ldots; -10\degres\right\}$}}
 %\addlegendimage{empty legend}\addlegendentry{\relsize{-1.5}{$D=165\degres$, $I=90\degres$}}
 %
\end{axis}
 %
\end{tikzpicture}
% ==============================================     
\end{document}

enter image description here

Best Answer

First of all: Instead of defining the functions 9 times for different parameter values, you can simply use \pgfplotsinvokeforeach{<list>}{ <code> } to loop over the values. That makes the code much more compact and maintainable.

Then, you can either use the contour prepared key, which tells PGFPlots to assume you're plotting contours, making it automatically place labels on the plot lines. The values used for this depend on the meta key, so if you set point meta=#1, they will correspond to the angles.

As you can see, the label placement isn't ideal. You can influence it a bit by setting contour/label distance=<value>, but I haven't been able to improve the placement much.

Instead, you could use a modified version of the approach outlined in Label plots in pgfplots without entering coordinates manually to place the labels with more control:


Code for first example

\documentclass[a4paper]{article}
        \usepackage[T1]{fontenc}
        \usepackage[utf8,applemac]{inputenc}
        \usepackage{lmodern, textcomp}
        \usepackage{mathrsfs,bm}
        \usepackage{amsmath,amssymb,amscd}
        \usepackage{comment,relsize}
        \usepackage[frenchb]{babel}
        % ==============================================
        \usepackage[babel=true,kerning=true]{microtype}%pour le package tikZ et les deux points``:''
        %%% TikZ packages.
        \usepackage{pgf,tikz}
        \usepackage{pgfplots}
        %
        \pagestyle{empty}

        \pgfplotsset{compat=newest}
        % ==============================================
        \begin{document}
        % ==============================================

        \begin{tikzpicture}
        % réglage de la grille de coordonnées
        \begin{axis}
          [ grid=major,
            no markers,
            axis on top,
            tick label style={font=\small},
            %extra y tick style={grid=major},
            %extra x tick style={grid=major}
            minor y tick num=1,
            minor x tick num=1,
            %smooth,
            xlabel={D{\'e}clinaison du miroir: $D_m$},
            ylabel={Distance z{\'e}nithale du miroir: $I_m$},
            xmin=0, xmax=180,
            ymin=-0, ymax=180,
            width=1\textwidth,
            height=1\textwidth,
            legend style={at={(0.02,0.97)},anchor=north west},
            legend pos=south east,
            legend cell align=left
           ]%
        % Création des graphes f(x,y)=0 via GNUPLOT
        \pgfplotsinvokeforeach{10,20,...,90}{
            \addplot [ contour prepared, point meta=#1, contour/label distance=10cm, contour/draw color=red,  no markers, red, raw gnuplot, thick, empty line = jump ]%
                gnuplot {%
                    set contour base;
                    set cntrparam levels discrete 0.0000;
                    unset surface;
                    set view map;
                    set grid;
                    set isosamples 200;
                    set xrange [0:180];
                    set yrange [0:180];
                    D=pi/180*165;           %<<--------- ICI
                    I=pi/180*90;            %<<--------- ICI
                    k=tan(pi/180*#1);
                    f(x,y)= k*(cos(D) + cos((D-2*pi/180*x)) * tan(pi/180*y)**2) - 2*sin(I) * sin(pi/180*x) * tan(pi/180*y)-cos(I) * (sin(D)+sin((D-2*pi/180*x)) * tan(pi/180*y)**2);
            splot f(x,y);
            };%
         }
        %
        \pgfplotsinvokeforeach{-10,-20,...,-80}{
            \addplot [ contour prepared, contour/label distance=10cm, contour/draw color=blue, point meta=#1, no markers, blue, raw gnuplot, thick, empty line = jump ]%
                gnuplot {%
                    set contour base;
                    set cntrparam levels discrete 0.0000;
                    unset surface;
                    set view map;
                    set grid;
                    set isosamples 200;
                    set xrange [0:180];
                    set yrange [0:180];
                    D=pi/180*165;           %<<--------- ICI
                    I=pi/180*90;            %<<--------- ICI
                    k=tan(pi/180*#1);
                    f(x,y)= k*(cos(D) + cos((D-2*pi/180*x)) * tan(pi/180*y)**2) - 2*sin(I) * sin(pi/180*x) * tan(pi/180*y)-cos(I) * (sin(D)+sin((D-2*pi/180*x)) * tan(pi/180*y)**2);
            splot f(x,y);
            };%
         }
\end{axis}
 %
\end{tikzpicture}
% ==============================================     
\end{document}

Code for second example

\documentclass[a4paper]{article}
        \usepackage[T1]{fontenc}
        \usepackage[utf8,applemac]{inputenc}
        \usepackage{lmodern, textcomp}
        \usepackage{mathrsfs,bm}
        \usepackage{amsmath,amssymb,amscd}
        \usepackage{comment,relsize}
        \usepackage[frenchb]{babel}
        % ==============================================
        \usepackage[babel=true,kerning=true]{microtype}%pour le package tikZ et les deux points``:''
        %%% TikZ packages.
        \usepackage{pgfplots}
        \usetikzlibrary{intersections}
        %
        \pagestyle{empty}

        \pgfplotsset{compat=newest}
        % ==============================================
        \begin{document}
        % ==============================================
\pgfkeys{
    /pgfplots/linelabel/.style args={#1:#2}{
        name path global=labelpath,
        execute at end plot={
            \path [name path global = labelpositionline]
                (rel axis cs:#1,0) -- (rel axis cs:#1,1);
            \path [name intersections={of=labelpath and labelpositionline, total=\total}]
                (intersection-\total) node [fill=white,inner xsep=1pt, inner ysep=0pt, font=\small] {#2};
        }
    }
}

        \begin{tikzpicture}
        % réglage de la grille de coordonnées
        \begin{axis}
          [ grid=major,
            no markers,
            axis on top,
            tick label style={font=\small},
            %extra y tick style={grid=major},
            %extra x tick style={grid=major}
            minor y tick num=1,
            minor x tick num=1,
            %smooth,
            xlabel={D{\'e}clinaison du miroir: $D_m$},
            ylabel={Distance z{\'e}nithale du miroir: $I_m$},
            xmin=0, xmax=180,
            ymin=-0, ymax=180,
            width=1\textwidth,
            height=1\textwidth,
            legend style={at={(0.02,0.97)},anchor=north west},
            legend pos=south east,
            legend cell align=left
           ]%
        % Création des graphes f(x,y)=0 via GNUPLOT
        \pgfplotsinvokeforeach{10,20,...,90}{
            \pgfmathsetmacro\labelpos{#1/200}
            \addplot [ linelabel=0.5:#1,no markers, red, raw gnuplot, thick, empty line = jump ]%
                gnuplot {%
                    set contour base;
                    set cntrparam levels discrete 0.0000;
                    unset surface;
                    set view map;
                    set grid;
                    set isosamples 200;
                    set xrange [0:180];
                    set yrange [0:180];
                    D=pi/180*165;           %<<--------- ICI
                    I=pi/180*90;            %<<--------- ICI
                    k=tan(pi/180*#1);
                    f(x,y)= k*(cos(D) + cos((D-2*pi/180*x)) * tan(pi/180*y)**2) - 2*sin(I) * sin(pi/180*x) * tan(pi/180*y)-cos(I) * (sin(D)+sin((D-2*pi/180*x)) * tan(pi/180*y)**2);
            splot f(x,y);
            };%
         }
        %
        \pgfplotsinvokeforeach{-10,-20,...,-80}{
            \addplot [ linelabel=0.5:#1, no markers, blue, raw gnuplot, thick, empty line = jump ]%
                gnuplot {%
                    set contour base;
                    set cntrparam levels discrete 0.0000;
                    unset surface;
                    set view map;
                    set grid;
                    set isosamples 200;
                    set xrange [0:180];
                    set yrange [0:180];
                    D=pi/180*165;           %<<--------- ICI
                    I=pi/180*90;            %<<--------- ICI
                    k=tan(pi/180*#1);
                    f(x,y)= k*(cos(D) + cos((D-2*pi/180*x)) * tan(pi/180*y)**2) - 2*sin(I) * sin(pi/180*x) * tan(pi/180*y)-cos(I) * (sin(D)+sin((D-2*pi/180*x)) * tan(pi/180*y)**2);
            splot f(x,y);
            };%
         }
\end{axis}
 %
\end{tikzpicture}
% ==============================================     
\end{document}