von Mac-Cherony » Mi 13. Nov 2013, 17:42
Vielen Dank für deinen Ansatz Elke, leider funktioniert das nicht immer so allgemein für andere Werte.
Als Übergangslösung generiere ich mir nun die Flächen mit Matlab, da ich meine Daten auch dort erzeuge.
Mit dem Ergebnis stimmt allerdings auch noch etwas nicht. Interessanterweise wird für jede Fläche die ich plotte auch ein Punkt bei y=0 geplottet, der allerdings nicht in den Daten vorhanden ist. Gibt es eine einfache Lösung dafür diesen Punkt nicht zu plotten, so dass wirklich nur die Fläche gefüllt wird?
Als vergleich habe ich einen Plot eingefügt, der direkt in pgfplots erzeugt wird.
Hier ist der Matlabcode mit dem ich die Daten generiere:
x = [-2:0.01:2]';
y1 = [0.2*x.^3-.05*x.^2+0.2];
y2 = [0.55*x+.13];
Data=[x(:) y1(:) y2(:)];
Diff=y1-y2;
dlmwrite('Data.dat', Data, 'delimiter','\t');
Index=['A';'B';'C';'D';'E';'F';'G';'H';];
VZW = find(diff(sign(Diff)) ~=0);
if isempty(VZW)==1;
VZW=length(x);
else
end
X=x(1:VZW(1));
Y1=y1(1:VZW(1));
Y2=y2(1:VZW(1));
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(1)>0
filename=strcat('Data',Index(1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
VZW = find(diff(sign(Diff)) ~=0);
if isempty(VZW)==1;
VZW=length(x)-1;
else
end
for n=1:length(VZW)
if n<length(VZW)
X=x(VZW(n):VZW(n+1));
Y1=y1(VZW(n):VZW(n+1));
Y2=y2(VZW(n):VZW(n+1));
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(VZW(n)+1)>0
filename=strcat('Data',Index(n+1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(n+1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
else
X=x(VZW(n):end);
Y1=y1(VZW(n):end);
Y2=y2(VZW(n):end);
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(VZW(n)+1)>0
filename=strcat('Data',Index(n+1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(n+1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
end
end
und hier das Minimalbeispiel in LaTeX:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [xmin=-2,xmax=2]
\addplot [draw=none, fill=red] file {DataA_neg.dat} \closedcycle;
\addplot [draw=none, fill=green] file {DataB_pos.dat} \closedcycle;
\addplot [draw=none, fill=red] file {DataC_neg.dat} \closedcycle;
\addplot [draw=none, fill=green] file {DataD_pos.dat} \closedcycle;
\addplot [] table[x index={0}, y index={1}] {Data.dat};
\addplot [blue] table[x index={0}, y index={2}] {Data.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=-2,xmax=2,domain=-2:2,samples=50,stack plots=y]
\addplot+[mark=none, blue] {.55*x+.13};
\addplot+[mark=none,fill=green,draw=black] {max(.2*x^3-.05*x^2+.2-(.55*x+.13),0)} \closedcycle;
\addplot+[mark=none,fill=red,draw=black] {min(.2*x^3-.05*x^2+.2-(.55*x+.13),0)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
Vielen Dank für deinen Ansatz Elke, leider funktioniert das nicht immer so allgemein für andere Werte.
Als Übergangslösung generiere ich mir nun die Flächen mit Matlab, da ich meine Daten auch dort erzeuge.
Mit dem Ergebnis stimmt allerdings auch noch etwas nicht. Interessanterweise wird für jede Fläche die ich plotte auch ein Punkt bei y=0 geplottet, der allerdings nicht in den Daten vorhanden ist. Gibt es eine einfache Lösung dafür diesen Punkt nicht zu plotten, so dass wirklich nur die Fläche gefüllt wird?
Als vergleich habe ich einen Plot eingefügt, der direkt in pgfplots erzeugt wird.
Hier ist der Matlabcode mit dem ich die Daten generiere:
[code]
x = [-2:0.01:2]';
y1 = [0.2*x.^3-.05*x.^2+0.2];
y2 = [0.55*x+.13];
Data=[x(:) y1(:) y2(:)];
Diff=y1-y2;
dlmwrite('Data.dat', Data, 'delimiter','\t');
Index=['A';'B';'C';'D';'E';'F';'G';'H';];
VZW = find(diff(sign(Diff)) ~=0);
if isempty(VZW)==1;
VZW=length(x);
else
end
X=x(1:VZW(1));
Y1=y1(1:VZW(1));
Y2=y2(1:VZW(1));
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(1)>0
filename=strcat('Data',Index(1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
VZW = find(diff(sign(Diff)) ~=0);
if isempty(VZW)==1;
VZW=length(x)-1;
else
end
for n=1:length(VZW)
if n<length(VZW)
X=x(VZW(n):VZW(n+1));
Y1=y1(VZW(n):VZW(n+1));
Y2=y2(VZW(n):VZW(n+1));
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(VZW(n)+1)>0
filename=strcat('Data',Index(n+1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(n+1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
else
X=x(VZW(n):end);
Y1=y1(VZW(n):end);
Y2=y2(VZW(n):end);
X = [X,flipud(X)];
Y = [Y1,flipud(Y2)];
if Diff(VZW(n)+1)>0
filename=strcat('Data',Index(n+1,:),'_pos.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
else
filename=strcat('Data',Index(n+1,:),'_neg.dat');
dlmwrite(filename, [X(:) Y(:)], 'delimiter','\t');
end
end
end
[/code]
und hier das Minimalbeispiel in LaTeX:
[code]
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [xmin=-2,xmax=2]
\addplot [draw=none, fill=red] file {DataA_neg.dat} \closedcycle;
\addplot [draw=none, fill=green] file {DataB_pos.dat} \closedcycle;
\addplot [draw=none, fill=red] file {DataC_neg.dat} \closedcycle;
\addplot [draw=none, fill=green] file {DataD_pos.dat} \closedcycle;
\addplot [] table[x index={0}, y index={1}] {Data.dat};
\addplot [blue] table[x index={0}, y index={2}] {Data.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=-2,xmax=2,domain=-2:2,samples=50,stack plots=y]
\addplot+[mark=none, blue] {.55*x+.13};
\addplot+[mark=none,fill=green,draw=black] {max(.2*x^3-.05*x^2+.2-(.55*x+.13),0)} \closedcycle;
\addplot+[mark=none,fill=red,draw=black] {min(.2*x^3-.05*x^2+.2-(.55*x+.13),0)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
[/code]