下面显示了一个黑色的蛋形丘和一系列由模拟外部数据构建的“红云”蛋形丘。
\documentclass[pstricks,dvipsnames]{standalone}
\usepackage{pst-plot,filecontents,siunitx}
\begin{filecontents*}{test.data}
60 0
70 2
80 8
90 24
100 56
110 86
120 110
130 120
\end{filecontents*}
\newpsstyle{mygrid}
{
Dy=10,
Dx=10,
Ox=0,
%tickwidth=1.2pt,
subticksize=1,
xsubticks=5,
ysubticks=5,
subtickcolor=lightgray,
%subtickwidth=.8pt,
xAxisLabel=\huge Mass (\si{\gram}),
xAxisLabelPos={c,-7},
yAxisLabel=\huge Cumulative Frequency,
yAxisLabelPos={-13,c},
llx=-2,
lly=-2,
urx=1,
ury=1,
}
\readdata{\mydata}{test.data}
\makeatletter
\pst@def{PreparePoints}<{% on stack are [ x y x y ...
counttomark /m exch def
] /DATA ED % save array
m 2 div cvi 2 mod 0 gt % odd or even pair of coordinates
{ 101 /Median ED } % odd
{ 101 /Median ED} % even
ifelse
/n 0 def
[
DATA length 2 div cvi {
DATA n get Median sub k mul Median add DATA n 1 add get
/n n 2 add def
} repeat
}>
\makeatother
\begin{document}
\begin{psgraph}[style=mygrid,xticksize=0 120,yticksize=0 200](0,0)(200,120){20cm}{20cm}
\multido{\r=-2.40+0.30}{17}{%
\pstVerb{/k \r\space def }%
\listplot[plotstyle=cspline,linecolor=red,linewidth=1pt,showpoints]{\mydata}}
\pstVerb{/k 1 def }%
\listplot[plotstyle=cspline,linecolor=black,linewidth=1pt,showpoints]{\mydata}
\end{psgraph}
\end{document}
我必须在Red-Cloud
答案中对中位数 101(用我的眼睛粗略估计)进行硬编码,这样我才得到以下结果。
问题
如何动态计算中位数?
答案1
中位数计算的代码可以优化,但我今天没有时间:
\documentclass[pstricks,dvipsnames]{standalone}
\usepackage{pst-plot,filecontents,siunitx}
\begin{filecontents*}{test.data}
60 0
70 2
80 8
90 24
100 56
110 86
120 110
130 120
\end{filecontents*}
\newpsstyle{mygrid}
{
Dy=10,
Dx=10,
Ox=0,
tickwidth=1.2pt,
subticksize=1,
xsubticks=2,
ysubticks=2,
subtickcolor=gray,
subtickwidth=.8pt,
xAxisLabel=\huge Mass (\si{\gram}),
xAxisLabelPos={c,-7},
yAxisLabel=\huge Cumulative Frequency,
yAxisLabelPos={-12,c},
llx=-2,
lly=-2,
urx=1,
ury=1,
}
\readdata{\mydata}{test.data}
\makeatletter
\pst@def{PreparePoints}<{% on stack are [ x y x y ...
counttomark /m exch def
m 2 div cvi /n exch def
] /DATA exch def % save array
DATA 1 get /yMin exch def DATA m 1 sub get /yMax exch def
yMin yMax add 2 div /yMean exch def
0 2 m 2 sub {% get left x value
/loopVar exch def
DATA loopVar 1 add get yMean lt
{ /x0 DATA loopVar get def /y0 DATA loopVar 1 add get def } if
} for
m 1 sub -2 1 {% get right x value
/loopVar exch def
DATA loopVar get yMean gt
{ /x1 DATA loopVar 1 sub get def /y1 DATA loopVar get def } if
} for
x1 x0 sub y1 y0 sub div yMean y0 sub mul x0 add
/Median exch def Median ==
/n 0 def
[
DATA length 2 div cvi {
DATA n get Median sub k mul Median add DATA n 1 add get
/n n 2 add def
} repeat
}>
\makeatother
\begin{document}
\begin{psgraph}[style=mygrid,xticksize=0 120,yticksize=0 200](0,0)(200,120){15cm}{20cm}
\multido{\rk=1.1+0.3,\iB=100+-10}{6}{%
\pstVerb{/k \rk\space def }
\listplot[plotstyle=cspline,linecolor=red!\iB,linewidth=2.4pt,showpoints]{\mydata}}
\pstVerb{/k 1 def }
\listplot[plotstyle=cspline,linecolor=black,linewidth=2.4pt,showpoints]{\mydata}
\end{psgraph}
\end{document}