我已经走到这一步了,但我无法独自继续前进。我有以下情节:
它是使用此代码生成的:
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=0, ymax=0.1, ytick={0,0.083}, ylabel=$f(x)$,
xmin=5, xmax=19, xtick={5, 6, 7, 8, 17, 18}, xlabel=$x$,
domain=-pi:pi,samples=101, % added
]
\addplot[domain=6:8,gray,name path=A] {0.083}; % actual curve
\addplot[draw=none,name path=B,domain=6:8] {0};
\addplot[domain=6:18,gray,name path=fullA]{0.083}; %total uniform area
\addplot[draw=none,name path=fullb,domain=6:18]{0};
\addplot[gray] fill between[of=A and B,soft clip={domain=6:8}]; %filling
coordinates {
(6, 0)
(6, 0.083)
(8, 0)
(8, 0.083)
(18, 0)
(18, 0.083)
};
\end{axis}
\end{tikzpicture}
\end{document}
我想:
1)将与 0.083 相关的刻度标记改为 1/12
2)从(18,0)到(18,0.083)的垂直灰线(该图应看起来像矩形内的矩形)
3) 标签 x 和 f(x) 位于轴的外侧而不是内侧(位于 x 刻度线下方;位于 y 刻度线左侧)。
我怎样才能做到这些事?
答案1
pgf
确实有将数字打印为分数的选项,所以你可以这样做yticklabel style={/pgf/number format/frac denom=12,/pgf/number format/frac}
(感谢薛定谔的猫提到了第一个)。或者你可以手动执行yticklabels={$0$,$\frac{1}{12}$}
- 在实现这一目标的多种方法中,您实际上只需在绘制水平线
-- (18,0)
的末尾添加即可。\addplot
(顺便注意,您实际上可以直接输入 1/12 而不是 0.083,如下例所示。)
\documentclass[border=5mm,12pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom, axis y line=left,
ymin=0, ymax=0.1, ytick={0,1/12}, ylabel=$f(x)$,
xmin=5, xmax=19, xtick={5, 6, 7, 8, 17, 18}, xlabel=$x$,
domain=-pi:pi,samples=101, % added
yticklabel style={
/pgf/number format/frac denom=12,
/pgf/number format/frac
} % automatic formatting
%yticklabels={$0$,$\frac{1}{12}$} % or do it manually instead
]
\addplot[domain=6:8,gray,name path=A] {1/12}; % actual curve
\addplot[draw=none,name path=B,domain=6:8] {0};
\addplot[domain=6:18,gray,name path=fullA]{1/12} -- (18,0); %total uniform area
\addplot[draw=none,name path=fullb,domain=6:18]{0};
\addplot[gray] fill between[of=A and B,soft clip={domain=6:8}]; %filling
coordinates {
(6, 0)
(6, 1/12)
(8, 0)
(8, 1/12)
(18, 0)
(18, 1/12)
};
\end{axis}
\end{tikzpicture}
\end{document}