我有一条频率曲线如下图所示:
我需要虚线来帮助估计得分在 42 到 76 之间的数字。
我的问题是
我需要在 x 轴上从 42 和 76 画两条垂直虚线。这两条线将与曲线相交,然后向左旋转 90°,并在两个相应的点处与 y 轴相交。
如果可能的话,使用 pin 命令或类似命令将显示虚线与 y 轴相交的两个相应点。
请帮我提供上述两个问题的 Tikz-PGF 代码。这是我目前的情况。
最小工作示例
\documentclass[tikz,border=3mm]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}
\begin {document}
\begin {figure}
\begin{tikzpicture}
\begin{axis}[tick align=outside,tick pos=lower,width=15cm,
xtick={10,20,...,100},xticklabel=\empty,
minor tick style={draw=none},x tick style={draw=none},
extra x ticks={9.5,19.5,...,99.5},
extra x tick style={draw,ultra thick,grid style={draw=none},x tick style={draw},
xticklabel=\pgfmathprintnumber\tick,
xticklabel style={rotate=30}},
axis x line=bottom,axis y line=left,
xmin=9,xmax=100,xlabel=Marks,
ymin=0,ymax=55,minor tick num=4,ylabel=Cumulative frequency,
grid=both,grid style={cyan},minor grid style={help lines,cyan},
table/create on use/cumulative frequency/.style={% cf. https://tex.stackexchange.com/a/198397
create col/expr={\pgfmathaccuma + \thisrow{frequency}}
}]
\addplot[red,thick,name path=plot,smooth,mark=+,mark options={color=black}]
table [x expr=9.5+10*\coordindex,
y=cumulative frequency]{
frequency
2
3
4
6
13
10
5
3
2
2
} coordinate[pos=1](pmax);
\path (0,0) coordinate (O)
(100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate
(tr);
\end{axis}
\draw[-stealth] (br) -- (tr);
\path (br) -- (br|-pmax) foreach \X in {0,20,...,100}
{coordinate[pos=\X/100] (p\X)
(p\X) edge[help lines]++ (\pgfkeysvalueof{/pgfplots/minor tick length},0)
node[pos=\X/100,right,xshift=\pgfkeysvalueof{/pgfplots/minor tick length}] {\X\%} };
\end{tikzpicture}
\end {figure}
\end {document}
注意:在下面的帖子中,情况正好相反。虚线起源于 y 轴,接触曲线并击中 x 轴。 如何使用 Tikz 和 PGF 而不是 pstricks 排版具有非整数 x 轴刻度的累积频率曲线
我尝试模仿该帖子中给出的解决方案,但没有成功。请帮助我,因为我还是新手。我很感激。
答案1
与上一个线程相同的策略有效:添加路径并计算交点。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[tick align=outside,tick pos=lower,width=11cm,
xtick={10,20,...,100},xticklabel=\empty,
minor tick style={draw=none},x tick style={draw=none},
extra x ticks={9.5,19.5,...,99.5},
extra x tick style={draw,ultra thick,grid style={draw=none},x tick style={draw},
xticklabel=\pgfmathprintnumber\tick,
xticklabel style={rotate=30}},
axis x line=bottom,axis y line=left,
xmin=9,xmax=100,xlabel=Marks,
ymin=0,ymax=55,minor tick num=4,ylabel=Cumulative frequency,
grid=both,grid style={cyan},minor grid style={help lines,cyan},
table/create on use/cumulative frequency/.style={% cf. https://tex.stackexchange.com/a/198397
create col/expr={\pgfmathaccuma + \thisrow{frequency}}
}]
\addplot[red,thick,name path=plot,smooth,mark=+,mark options={color=black}]
table [x expr=9.5+10*\coordindex,
y=cumulative frequency]{
frequency
2
3
4
6
13
10
5
3
2
2
} coordinate[pos=1](pmax);
\path (0,0) coordinate (O)
(100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate
(tr);
\pgfplotsinvokeforeach{42,76}{%
\path[name path=v#1] (#1,0) coordinate (x#1)-- (x#1|-pmax);
\path[name intersections={of=v#1 and plot,by=i#1}];}
\end{axis}
\draw[-stealth] (br) -- (tr);
\path (br) -- (br|-pmax) foreach \X in {0,20,...,100}
{coordinate[pos=\X/100] (p\X)
(p\X) edge[help lines]++ (\pgfkeysvalueof{/pgfplots/minor tick length},0)
node[pos=\X/100,right,xshift=\pgfkeysvalueof{/pgfplots/minor tick length}] {\X\%} };
\foreach \X in {42,76}
{\draw[dashed,semithick] (x\X) |- (br|-i\X) node[right]{$v_{\X}$};}
\end{tikzpicture}
\end{figure}
\end{document}
请注意,对于您选择的文档类别而言,该图太宽article
,因此我减小了宽度。(这些选项tikz,border=3mm
适用于独立版本,但不是类别的选项article
。)
附录:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[tick align=outside,tick pos=lower,width=11cm,
xtick={10,20,...,100},xticklabel=\empty,
minor tick style={draw=none},x tick style={draw=none},
extra x ticks={9.5,19.5,...,99.5},
extra x tick style={draw,ultra thick,grid style={draw=none},x tick style={draw},
xticklabel=\pgfmathprintnumber\tick,
xticklabel style={rotate=30}},
axis x line=bottom,axis y line=left,
xmin=9,xmax=100,xlabel=Marks,
ymin=0,ymax=55,minor tick num=4,ylabel=Cumulative frequency,
grid=both,grid style={cyan},minor grid style={help lines,cyan},
table/create on use/cumulative frequency/.style={% cf. https://tex.stackexchange.com/a/198397
create col/expr={\pgfmathaccuma + \thisrow{frequency}}
}]
\addplot[red,thick,name path=plot,smooth,mark=+,mark options={color=black}]
table [x expr=9.5+10*\coordindex,
y=cumulative frequency]{
frequency
2
3
4
6
13
10
5
3
2
2
} coordinate[pos=1](pmax);
\path (0,0) coordinate (O) (9.5,0) coordinate (O')
(100,0) coordinate (br) (100,\pgfkeysvalueof{/pgfplots/ymax}) coordinate
(tr);
\pgfplotsinvokeforeach{42,76}{%
\path[name path=v#1] (#1,0) coordinate (x#1)-- (x#1|-pmax);
\path[name intersections={of=v#1 and plot,by=i#1}];}
\end{axis}
\draw[-stealth] (br) -- (tr);
\path (br) -- (br|-pmax) foreach \X in {0,20,...,100}
{coordinate[pos=\X/100] (p\X)
(p\X) edge[help lines]++ (\pgfkeysvalueof{/pgfplots/minor tick length},0)
node[pos=\X/100,right,xshift=\pgfkeysvalueof{/pgfplots/minor tick length}] {\X\%} };
\foreach \X in {42,76}
{\draw[dashed,semithick] (x\X) |- (O'|-i\X) node[left]{$v_{\X}$};}
\end{tikzpicture}
\end{figure}
\end{document}