我如何访问ytick
pgfplots 内部计算的数据?
\pgfplots@ytick
具体来说,仅当在轴环境选项内进行手动定义时,该字段才为非空。包含内部的等效字段是什么ytick
?
我想自动重绘一些与 ymajorgrid 绘制的线相同的线,但稍微短一些。
\documentclass{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\pgfplotsset{compat = newest}
\makeatletter
\pgfplotsset{
execute at end axis={
\expandafter\pgfplotsinvokeforeach\expandafter{\pgfplots@ytick}{\draw[gray!50!white,thin] ({axis cs:\pgfplots@data@xmin,#1}) -- ({axis cs:\pgfplots@data@xmax,#1}); }
},
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[ %ymajorgrids,
%ytick={5400,5600,5800,6000,6200,6400} %with this line, it works, but I am looking for an automatic alternative
]
\addplot+[thick,mark size=2,error bars/.cd,y dir=both,y explicit ]
coordinates {
(7.846, 5614.5) +- (110., 110.)
(8.096, 5530.) +- (69., 69.)
(8.65, 5442.) +- (57., 57.)
};
\addlegendentry{a}
\addplot+[thick,mark=none]
coordinates {
(7.5, 6319.2)
(9.03, 5996.6)
};
\addlegendentry{b}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我不能说我理解这一切,但它确实有效。
\documentclass{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\pgfplotsset{compat = newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ %ymajorgrids,
%ytick={5400,5600,5800,6000,6200,6400} %with this line, it works, but I am looking for an automatic alternative
]
\addplot+[thick,mark size=2,error bars/.cd,y dir=both,y explicit ]
coordinates {
(7.846, 5614.5) +- (110., 110.)
(8.096, 5530.) +- (69., 69.)
(8.65, 5442.) +- (57., 57.)
};
\addlegendentry{a}
\addplot+[thick,mark=none]
coordinates {
(7.5, 6319.2)
(9.03, 5996.6)
};
\addlegendentry{b}
\makeatletter
\pgfplotsextra{
\let\pgfplots@prepared@tick@positions@major@=\pgfplots@prepared@tick@positions@major@y
\pgfplotsonlayer{background}{y grid style}%
\scope
\pgfplotstransformcoordinatex{\pgfplots@data@xmin}
\let\xmin=\pgfmathresult
\pgfplotstransformcoordinatex{\pgfplots@data@xmax}
\let\xmax=\pgfmathresult
\pgfplots@drawgridlines@INSTALLCLIP@onorientedsurf{y}% no obvious effect
\draw[/pgfplots/every major y grid]%
\pgfextra
\pgfplotslistforeach\pgfplots@prepared@tick@positions@major@\as\pgfplots@curgridpos{%
\expandafter\pgfplots@prepared@tick@pos@unpack\pgfplots@curgridpos
\pgfpathmoveto{\pgfplotspointonorientedsurfaceab{\pgfplots@tick}{\xmin}}%
\pgfpathlineto{\pgfplotspointonorientedsurfaceab{\pgfplots@tick}{\xmax}}%
}%
\endpgfextra;
\endscope
\endpgfplotsonlayer
}%
\makeatother
\end{axis}
\end{tikzpicture}
\end{document}