我想命名不同的轮廓,就像在地形卡上看到的那样。怎么做?我想用区分曲线的参数在不同的曲线上贴标签。我在 tikz 环境中用 gnuplot 创建了 10+10 条曲线。
这里提供的所有代码均来自此网站。非常感谢。有人能帮我在每条曲线上贴一个小标签(如 -10°、-20°、...、90°)来完成我的图表吗?
代码 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}
答案1
首先:您无需为不同的参数值定义 9 次函数,只需使用\pgfplotsinvokeforeach{<list>}{ <code> }
循环遍历值即可。这使得代码更加紧凑且更易于维护。
然后,您可以使用contour prepared
键,它告诉 PGFPlots 假设您正在绘制轮廓,使其自动在绘图线上放置标签。用于此目的的值取决于 键meta
,因此如果您设置point meta=#1
,它们将与角度相对应。
如您所见,标签位置并不理想。您可以通过设置来稍微影响它contour/label distance=<value>
,但我无法大大改善位置。
相反,您可以使用中概述的方法的修改版本在 pgfplots 中标记图,无需手动输入坐标放置标签时可以进行更多控制:
第一个例子的代码
\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}
第二个示例的代码
\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}