如何设置 psgraph 以获得 pspicture 和 psaxes 产生的相同输出?

如何设置 psgraph 以获得 pspicture 和 psaxes 产生的相同输出?

为了获得更平衡的图表(具有特定的指定度量大小,width乘以height),我通常还会在画布(又称图片)和轴周围指定一些偏移量或调整值,如下所示。请注意,添加注释只是为了简化我们的讨论。

在此处输入图片描述

POT例如AOT,代表图片偏移量(又称画布偏移)和A西斯分别为 ffset。画布大小和所有偏移量通常以厘米为单位。

以下是我的 MWE。为了简单起见,已删除注释相关代码。

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl,pstricks-add}
\usepackage[nomessages]{fp}
\newcommand\const[3][3]{%
    \expandafter\FPeval\csname#2\endcsname{round(#3:#1)}%
    \pstVerb{/#2 \csname#2\endcsname\space def}%
}

% User defined data:
\def\f(#1){2.5*cos(2*#1)}% function to plot

\const{Width}{8}% total width in cm
\const{Height}{7}% total height in cm 

\const{POL}{-1.00}% pspicture left offset in cm
\const{POR}{ 0.75}% pspicture right offset in cm
\const{POB}{-1.00}% pspicture bottom offset in cm
\const{POT}{ 0.75}% pspicture top offset in cm

\const{AOL}{-0.50}% axis left offset in cm
\const{AOR}{ 0.75}% axis right offset in cm
\const{AOB}{-0.50}% axis bottom offset in cm
\const{AOT}{ 0.75}% axis top offset in cm

\const{DomL}{-2*pi}% domain left 
\const{DomR}{pi}% domain right 
\const{DomB}{-3}% domain bottom 
\const{DomT}{ 3}% domain top

\const[0]{TrigLabelBase}{2}% denominator for a fraction of pi

% Internal used constants:
\const{XUnit}{(Width-POR+POL-AOR+AOL)/(DomR-DomL)}
\const{YUnit}{(Height-POT+POB-AOT+AOB)/(DomT-DomB)}

\const{PicL}{(POL+AOL)/XUnit+DomL}
\const{PicR}{(POR+AOR)/XUnit+DomR}
\const{PicB}{(POB+AOB)/YUnit+DomB}
\const{PicT}{(POT+AOT)/YUnit+DomT}

\const{AxiL}{AOL/XUnit+DomL}
\const{AxiR}{AOR/XUnit+DomR}
\const{AxiB}{AOB/YUnit+DomB}
\const{AxiT}{AOT/YUnit+DomT}

\const{DeltaX}{pi/TrigLabelBase}

\psset{xunit=\XUnit,yunit=\YUnit,algebraic,plotpoints=500}


\begin{document}
\begin{pspicture}(\PicL,\PicB)(\PicR,\PicT)
    \psplot[linecolor=blue]{\DomL}{\DomR}{\f(x)}
    \psaxes
    [
        trigLabels=true,
        linecolor=gray,
        labelFontSize=\scriptscriptstyle,
        tickcolor=gray,
        ticksize=-1.5pt 1.5pt,
        xlabelsep=3pt,
        arrowscale=1,
        trigLabelBase=\TrigLabelBase,
        dx=\DeltaX,% must come before xunit to avoid getting a strange output!  
    ]{->}(0,0)(\AxiL,\AxiB)(\AxiR,\AxiT)[$x$,0][$y$,90]
\end{pspicture}
\end{document}

现在我知道psgraph可以根据绘制图形和轴的画布的指定公制尺寸(例如以厘米为单位)自动进行缩放。

问题是如何设置以获得与我上面使用和psgraph已经获得的相同结果?似乎是一个很有前途的语法糖,可以减少更多的击键!pspicturepsaxespsgraph

答案1

\documentclass[pstricks,border=0pt]{standalone}
\usepackage{pst-eucl,pstricks-add}
\usepackage[nomessages]{fp}
\newcommand\const[3][3]{%
    \expandafter\FPeval\csname#2\endcsname{round(#3:#1)}%
    \pstVerb{/#2 \csname#2\endcsname\space def}%
}

% User defined data:
\def\f(#1){2.5*cos(2*#1)}% function to plot

\const{Width}{8}% total width in cm
\const{Height}{7}% total height in cm 

\const{POL}{-1.00}% pspicture left offset in cm
\const{POR}{ 0.75}% pspicture right offset in cm
\const{POB}{-1.00}% pspicture bottom offset in cm
\const{POT}{ 0.75}% pspicture top offset in cm

\const{AOL}{-0.50}% axis left offset in cm
\const{AOR}{ 0.75}% axis right offset in cm
\const{AOB}{-0.50}% axis bottom offset in cm
\const{AOT}{ 0.75}% axis top offset in cm

\const{DomL}{-2*pi}% domain left 
\const{DomR}{pi}% domain right 
\const{DomB}{-3}% domain bottom 
\const{DomT}{ 3}% domain top

\const[0]{TrigLabelBase}{2}% denominator for a fraction of pi

% Internal used constants:
\const{XUnit}{(Width-POR+POL-AOR+AOL)/(DomR-DomL)}
\const{YUnit}{(Height-POT+POB-AOT+AOB)/(DomT-DomB)}

\const{PicL}{(POL+AOL)/XUnit+DomL}
\const{PicR}{(POR+AOR)/XUnit+DomR}
\const{PicB}{(POB+AOB)/YUnit+DomB}
\const{PicT}{(POT+AOT)/YUnit+DomT}

\const{AxiL}{AOL/XUnit+DomL}
\const{AxiR}{AOR/XUnit+DomR}
\const{AxiB}{AOB/YUnit+DomB}
\const{AxiT}{AOT/YUnit+DomT}

\const{DeltaX}{pi/TrigLabelBase}

\psset{xunit=\XUnit,yunit=\YUnit,algebraic,plotpoints=500}


\begin{document}

\begin{pspicture}(\PicL,\PicB)(\PicR,\PicT)
    \psplot[linecolor=blue]{\DomL}{\DomR}{\f(x)}
    \psaxes
    [
        trigLabels=true,
        linecolor=gray,
        labelFontSize=\scriptscriptstyle,
        tickcolor=gray,
        ticksize=-1.5pt 1.5pt,
        xlabelsep=3pt,
        arrowscale=1,
        trigLabelBase=\TrigLabelBase,
        dx=\DeltaX,% must come before xunit to avoid getting a strange output!  
    ]{->}(0,0)(\AxiL,\AxiB)(\AxiR,\AxiT)[$x$,0][$y$,90]
\end{pspicture}

\psset{unit=1cm}
\psset{llx=\POL cm,urx=\POR cm,lly=\POB cm,ury=\POT cm}

\begin{psgraph}[
        trigLabels=true,
        linecolor=gray,
        labelFontSize=\scriptscriptstyle,
        tickcolor=gray,
        ticksize=-1.5pt 1.5pt,
        xlabelsep=3pt,
        arrowscale=1,
        trigLabelBase=\TrigLabelBase,
        dx=\DeltaX]{->}(0,0)(\AxiL,\AxiB)(\AxiR,\AxiT)
         {\dimexpr \Width cm +\POL cm - \POR cm}
         {\dimexpr\Height cm +\POB cm - \POT cm}
    \psplot[linecolor=blue]{\DomL}{\DomR}{\f(x)}
\end{psgraph}

\end{document}

相关内容