图形的定位和大小

图形的定位和大小

我想呈现与另一个图像相关的图形:当我在环境中执行此操作时\scope,我无法正确定位图形或更改其大小,我也无法在 y 轴上添加单个值(值 1)。

\documentclass[border=2mm]{standalone}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\usepackage{pgfplots}
\usepackage{pgf-spectra}

\usepgfplotslibrary{fillbetween}
% Preamble: 
\pgfplotsset{compat=newest}
\usetikzlibrary{math}
\tikzmath{
  function sinc2(\x) {
    if  abs(\x) < .001 then { % (|x| < .001) ~ (x = 0)
      return 1;
    } else {
      return (sin(\x r)/\x)*(sin(\x r)/\x);
    };
  };
}
%--------------------------------------------------------
\begin{document}
%--------------------------------------------------------
\begin{tikzpicture}
    \begin{scope}
        \fill (-3,-3) rectangle (3,3);
        \fill [white] (-0.05,-2) rectangle (0.05,2);
\end{scope}
    \begin{scope}[xshift=7cm]
        \begin{axis}[yscale=2]%
    [grid=both,
     minor tick num=1,
     grid style={line width=.1pt, draw=gray!10},
     major grid style={line width=.2pt,draw=gray!50},
     axis lines=middle,
        xtick={-9.42477796, -6.28318, -3.14159, 3.14159, 6.28318, 9.42477796
    },
        xticklabels={$-3\pi$, $-2\pi$,  $-\pi$,$\pi$,  $2\pi$, $3\pi$}
    ]
    \addplot[domain=-4*pi:4*pi,samples=200,smooth,red] {sinc2(x))};
  \end{axis}
\end{scope}


\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

随着矩形垂直坐标的变化:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{math}
\tikzmath{
  function sinc2(\x) {
    if  abs(\x) < .001 then { % (|x| < .001) ~ (x = 0)
      return 1;
    } else {
      return (sin(\x r)/\x)*(sin(\x r)/\x);
    };
  };
}
%--------------------------------------------------------
\begin{document}
%--------------------------------------------------------
\begin{tikzpicture}               
    \fill (-3,0) rectangle (3,6); % <---
    \fill[white] (-0.05,1) rectangle (0.05,5); % <---

\begin{axis}[xshift=4cm,          % <---
    axis lines=middle,
    grid=both,
    minor tick num=1,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
    xtick={-3*pi, -2*pi, -pi, pi, 2*pi, 3*pi},
    xticklabels={$-3\pi$, $-2\pi$, $-\pi$,$\pi$, $2\pi$, $3\pi$}
    ]
    \addplot[domain=-4*pi:4*pi,samples=200,red] {sinc2(x))};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用此代码:

\documentclass[border=2mm]{standalone}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\usepackage{pgfplots}
\usepackage{pgf-spectra}

\usepgfplotslibrary{fillbetween}
% Preamble: 
\pgfplotsset{compat=newest}
\usetikzlibrary{math}
\tikzmath{
    function sinc2(\x) {
        if  abs(\x) < .001 then { % (|x| < .001) ~ (x = 0)
            return 1;
        } else {
            return (sin(\x r)/\x)*(sin(\x r)/\x);
        };
    };
}
%--------------------------------------------------------
\begin{document}
    %--------------------------------------------------------
    \begin{tikzpicture}
        \begin{scope}
            \fill (-3,-3) rectangle (3,3);
            \fill [white] (-0.05,-2) rectangle (0.05,2);
        \end{scope}
        \begin{scope}[xshift=3.5cm,yshift=-3cm]
            \begin{axis}%[yscale=2]%
                [grid=both,
                minor tick num=1,
                grid style={line width=.1pt, draw=gray!10},
                major grid style={line width=.2pt,draw=gray!50},
                axis lines=middle,
                xtick={-9.42477796, -6.28318, -3.14159, 3.14159, 6.28318, 9.42477796
                },
                xticklabels={$-3\pi$, $-2\pi$,  $-\pi$,$\pi$,  $2\pi$, $3\pi$}
                ]
                \addplot[domain=-4*pi:4*pi,samples=200,smooth,red] {sinc2(x))};
            \end{axis}
        \end{scope}
        
        
    \end{tikzpicture}
\end{document}

你有这个输出:

在此处输入图片描述

答案3

不要为此使用范围,但是anchorat。你没有写出你希望它们如何放置,所以我猜测。

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\fill (-3,-3) rectangle (3,3);
\fill[white] (-0.05,-2) rectangle (0.05,2);
\begin{axis}[ 
anchor=left of origin, at={(4cm,-3cm)},
grid=both,
minor tick num=1,
grid style={gray!20},
axis lines=middle,
xtick={-3*pi, -2*pi, -pi, pi, 2*pi, 3*pi},
xticklabels={$-3\pi$, $-2\pi$, $-\pi$, $\pi$, $2\pi$, $3\pi$},
trig format plots=rad,
]
\addplot[domain=-4*pi:4*pi, samples=100, smooth, red] {abs(x)>0.001?(sin(x)/x)^2:1};
\end{axis}
\end{tikzpicture}
\end{document}

黑色矩形和图形

如果希望轴的高度与矩形的高度相同,则可以使用以下选项:

scale only axis=true, height=6cm,

相关内容