我需要修改这段代码的哪些部分才能获得简单的注释?
\begin{tikzpicture}
\begin{axis}[
xtick={45,46,...,55},
ytick={0,0.1,...,1},
width=18cm,
height=10cm,
grid=major,
minor x tick num=9,
%minor y tick num={1},
ymin=0,
ymax=1,
xmin=45,
xmax=55,
]
\addplot[mark=none, blue, line width=1.5pt] coordinates {(45,0)(47,0.1)(49,0.35)(51,0.775)(53,0.875)(55,1)};
\addplot[mark=none, dashed, red] coordinates {(0,0.5)(49.7,0.5)(49.7,0.5)(49.7,0)};
\node at (axis cs:49.7,-0.15){Me};
\addplot[mark=none, dashed, red] coordinates {(0,0.25)(48.2,0.25)(48.2,0.25)(48.2,0)};
\node at (axis cs:48.2,-0.15){$Q_1$};
\addplot[mark=none, dashed, red] coordinates {(0,0.75)(50.9,0.75)(50.9,0.75)(50.9,0)};
\node at (axis cs:50.9,-0.15){$Q_3$};
\end{axis}
\end{tikzpicture}
答案1
正如 Torbjørn 在在问题下方评论解决您的问题的“快速解决方案”是简单地使用clip mode=individual
,但“您的”方式 - 在我看来 - 不是添加注释的理想解决方案。
这里我介绍另一种使用的方法extra ticks
。有关更多详细信息,请查看代码中的注释。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use `compat' level 1.11 or higher so you don't need to add
% `axis cs:' to each TikZ coordinate
compat=1.11,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% % not needed, because this is the default with this scales
% xtick={45,46,...,55},
% and instead of applying the `ytick`s just give the "distance"
ytick distance=0.1,
%
width=18cm,
height=10cm,
grid=major,
minor x tick num=9,
ymin=0,
ymax=1,
xmin=45,
xmax=55,
%
% if you don't want to draw markers (anywhere), disable it globally
no markers,
% where do you want to draw the extra labels ...
extra x ticks={48.2,49.7,50.9},
extra x tick style={
% and what should the labels state
xticklabels={$Q_1$,Me,$Q_3$},
% adjust the style so it fits your needs ...
% you don't want to draw (extra) grid lines there
grid=none,
% you also don't want to draw extra ticks there
major tick length=0pt,
% and the labels should be moved below the "normal" ticklabels
xticklabel style={
yshift=-3ex,
},
% to align the labels on the same baseline
% (for your current labels that doesn't change anything,
% but if you change e.g. "Me" to "me" and "$Q_3$" to
% "${Q_3^2}^2$", you will see the difference, if you comment
% the next line)
typeset ticklabels with strut,
},
]
\addplot[mark=none, blue, line width=1.5pt] coordinates {
(45,0)(47,0.1)(49,0.35)(51,0.775)(53,0.875)(55,1)
};
% you only need _3_ coordinates to plot the dashed lines
% and so you don't need to adapt the "lower" values when changing the
% axis limits, just use them directly in the coordinates
\addplot [dashed, red] coordinates {
(\pgfkeysvalueof{/pgfplots/xmin},0.25)
(48.2,0.25)
(48.2,\pgfkeysvalueof{/pgfplots/ymin})
};
% but you can also do it with _2_ coordinates using TikZ's `\draw' command
\draw [dashed, red]
(\pgfkeysvalueof{/pgfplots/xmin},0.5) -| (49.7,\pgfkeysvalueof{/pgfplots/ymin})
;
\draw [dashed, red]
(\pgfkeysvalueof{/pgfplots/xmin},0.75) -| (50.9,\pgfkeysvalueof{/pgfplots/ymin})
;
\end{axis}
\end{tikzpicture}
\end{document}