Anfügen von beliebigem Code an eine pgfplots-Achsenumgebung, bevor Kurve und Achsen gezeichnet werden

Anfügen von beliebigem Code an eine pgfplots-Achsenumgebung, bevor Kurve und Achsen gezeichnet werden

Es gab einige Verwirrung darüber, was ich wirklich wollte, also füge ich den tatsächlichen Anwendungsfall hier ein. Das MWE und das entsprechende Bild finden Sie unten im Beitrag.

Ich habe ein paar schick aussehende Diagramme erstellt, darunter benutzerdefinierte Gitternetzlinien. Einige der Gitternetzlinien reichen nicht bis zu meiner benutzerdefinierten Achse. Wie kann ich das am besten hinbekommen?

http://i.imgur.com/kIU9gjT.png

\documentclass{standalone}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,shapes, positioning}
\tikzset{
  shadowed/.style={preaction={
      transform canvas={shift={(2pt,-1pt)}},draw opacity=.2,#1,preaction={
        transform canvas={shift={(4pt,-1.75pt)}},draw opacity=.1,#1,preaction={
          transform canvas={shift={(6pt,-2.5pt)}},draw opacity=.05,#1,preaction={
          transform canvas={shift={(8pt,-3.25pt)}},draw opacity=.025,#1,
        }
        }}}},
}
\makeatletter
\def\pgfplotsdataxmin{\pgfplots@data@xmin}
\def\pgfplotsdataxmax{\pgfplots@data@xmax}
\def\pgfplotsdataymin{\pgfplots@data@ymin}
\def\pgfplotsdataymax{\pgfplots@data@ymax}
\makeatother
\pgfplotsset{
    range frame/.style={
        tick align=outside,
        axis line style={opacity=0},
        after end axis/.code={
            \draw[cyan,thick,double=white,double distance=1.4pt,line cap=round,rounded corners] ({rel axis cs:0,0}-|{axis cs:\pgfplotsdataxmax,0}) -- ({rel axis cs:0,0}-|{axis cs:\pgfplotsdataxmin,0}) --   ({rel axis cs:0,0}|-{axis cs:0,\pgfplotsdataymin}) -- ({rel axis cs:-.1,0}|-{axis cs:0,\pgfplotsdataymax});
        }
    }
}

\begin{document}
\begin{tikzpicture}[scale=2]
\begin{axis}[range frame,
        domain=25.29325198:97,
        axis lines*=left,
        yticklabel=\empty,
        xticklabels=\empty,
        xtick style={draw=none},
        ytick style={draw=none},
extra description/.code={%
\node[] at (axis cs:6,60) {$60$};
\node[] at (axis cs:8.6,40) {$40$};
\node[] at (axis cs:11.5,20) {$20$};
\node[] at (axis cs:15,0) {$0$};
\node[] at (axis cs:25,-11.3) {$25$};
\node[] at (axis cs:50,-11.3) {$50$};
\node[] at (axis cs:75,-11.3) {$75$};
\draw[ultra thin,lightgray] (axis cs:25,-9) -- (axis cs:25,-7);
\draw[ultra thin,lightgray] (axis cs:50,-9) -- (axis cs:50,-7.5);
\draw[ultra thin,lightgray] (axis cs:75,-9) -- (axis cs:75,-7.5);
\node[] at (axis cs:\pgfplotsdataxmin-15.5,\pgfplotsdataymax+4.4) {$m_\mathrm{gr}$};
\node[] at (axis cs:\pgfplotsdataxmax+5.5,\pgfplotsdataymin-7) {$m_\mathrm{f}$};
}
]
\draw[ultra thin,lightgray] (axis cs:-8.05,20) -- (axis cs:\pgfplotsdataxmax,20);
\draw[ultra thin,lightgray] (axis cs:-8.05,0) -- (axis cs:\pgfplotsdataxmax,0);
\draw[ultra thin,lightgray] (axis cs:-8.05,40) -- (axis cs:\pgfplotsdataxmax,40);
\draw[ultra thin,lightgray] (axis cs:-8.05,60) -- (axis cs:\pgfplotsdataxmax,60);
\addplot[shadowed={double=gray,draw=gray},thick,line cap=round,rounded corners, draw=purple,double=white,double distance=1.6pt,
]  {-23.73194+0.9382716*x};
\end{axis}
\end{tikzpicture}
\makeatother


\end{document}

Ursprünglicher Beitrag:Ich bin gezwungen, meine eigenen Gitternetzlinien zu erstellen, aber die Achsen überlappen die Gitternetzlinien, wenn ich den folgenden Code verwende. Dies führt dazu, dass ein Teil meiner Gitternetzlinien ignoriert wird. Ich möchte sie direkt hinter den Achsen zeichnen und meine angegebenen x-Koordinaten einhalten. Wie kann ich die Linien zeichnen, bevor die Achsen und die Kurve gezeichnet werden, aber ohne den überlappenden weißen Raum, der durch die Achsen entsteht?

http://i.imgur.com/8c8YjMh.png

\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\begin{document}
\begin{tikzpicture}[scale=2]
\begin{axis}[
extra description/.code={%preaction={ %preaction doesn't work
},%}
]
\draw[ultra thick,lightgray] (axis cs:-2,2) -- (axis cs:\pgfplots@data@xmax,2);
\draw[ultra thick,lightgray] (axis cs:.04,4) -- (axis cs:\pgfplots@data@xmax,4);
\draw[ultra thick,lightgray] (axis cs:0,6) -- (axis cs:\pgfplots@data@xmax,6);
\addplot[
] table {
dof l2_err level
.2 2.6 2
.4 2.3 4
.5 2.4 5
.6 1.1 6
.7 1.8 7
.8 4.6 8
.9 3.3 9
1 6.2 10
};
\end{axis}
\end{tikzpicture}
\makeatother
\end{document}

Antwort1

Sie können die execute at <begin/end> <certain events>Schlüssel für eine konsistentere Ausgabe verwenden. Anstelle des maximalen Datenpunkts können Sie auch verwenden, axis description csum die gesamte Achsenlänge abzurufen, unabhängig davon, wo sich die Datenpunkte befinden.

\documentclass{standalone}
\usepackage{pgfplots}
\makeatletter
\pgfplotsset{compat=1.11,
my extras/.style={
  execute at begin axis={
    \draw[ultra thick,lightgray] (axis cs:.08,2) -- (axis cs:\pgfplots@data@xmax,2);
    \draw[ultra thick,lightgray] (axis cs:.04,4) -- (axis cs:\pgfplots@data@xmax,4);
    \draw[ultra thick,lightgray] (axis cs:0,6) -- (axis cs:\pgfplots@data@xmax,6);
    }
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}[scale=2]
\begin{axis}[my extras]
\addplot+[ultra thick,
] table {
dof l2_err level
.2 2.6 2
.4 2.3 4
.5 2.4 5
.6 1.1 6
.7 1.8 7
.8 4.6 8
.9 3.3 9
1 6.2 10
};
\end{axis}
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Antwort2

Die Lösung der aktualisierten Frage scheint darin zu bestehen,

clip=false,

zur axisUmgebung. In Ihrer Version werden die manuellen horizontalen Gitterlinien außerhalb der ursprünglichen Größe der Achse abgeschnitten.

Auch die Enden der waagerechten Linien sollten Sie dann anpassen, zB:

\draw[ultra thin,lightgray] (axis cs:17,0) -- (axis cs:\pgfplotsdataxmax,0);
\draw[ultra thin,lightgray] (axis cs:14.3,20) -- (axis cs:\pgfplotsdataxmax,20);
\draw[ultra thin,lightgray] (axis cs:11.6,40) -- (axis cs:\pgfplotsdataxmax,40);
\draw[ultra thin,lightgray] (axis cs:9,60) -- (axis cs:\pgfplotsdataxmax,60);

thinZur besseren Sichtbarkeit habe ich in dieser Vorschau die Linienstärke geändert auf :Vorschau

verwandte Informationen