我对自己真正想要的东西有些困惑,所以我在这里附上了实际用例。MWE 和相应的图片位于文章底部。
我制作了一些看起来很漂亮的图表,其中使用了自定义网格线。有些网格线没有完全延伸到我自定义的轴。我最好的办法是什么?
\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}
原始帖子:我被迫生成自己的网格线,但当我使用下面的代码时,轴与网格线重叠。这导致我的部分网格线被忽略。我希望将它们直接绘制在轴后面并遵循我指定的 x 坐标。如何在绘制轴和曲线之前绘制线条,但不产生轴产生的重叠空白?
\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}
答案1
您可以使用execute at <begin/end> <certain events>
按键获得更一致的输出。此外,除了最大数据点之外,您还可以使用按键axis description cs
获得与数据点位置无关的整个轴长度。
\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}
答案2
更新后的问题的解决方案似乎是添加
clip=false,
到axis
环境中。在您的版本中,手动水平网格线被剪裁到轴的原始尺寸之外。
然后您还应该调整水平线的末端,例如:
\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);