填充曲線下區域會導致對齊問題

填充曲線下區域會導致對齊問題

我正在嘗試填充 sin(x)/x 曲線下的面積。這是我的程式碼:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows,shadows,positioning, calc, decorations.markings, hobby, quotes,angles,decorations.pathreplacing,intersections}
\usepgfplotslibrary{polar,colormaps,fillbetween}
\usepgflibrary{shapes.geometric}
\usepackage{xcolor}

\begin{document} 

\begin{figure}[ht]

\centering

\begin{tikzpicture}[scale=1.5,line width=1pt]
\begin{axis}[
color= black,
xmin=-1, 
xmax=7.9, 
ymin=-1, 
ymax=2, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
%xticklabels={}, 
%yticklabels={},
inner axis line style={stealth-stealth},
xlabel = {$x$},
ylabel = {$f(x)$},
ticks=none,
]

\addplot[black, opacity = 0, domain=0:10, name path = 1]{0};

\addplot[Tan, smooth, domain=0.01:7.5, samples = 300, name path=2, thick] {sin(deg(x))/x}; 

%\addplot[Tan, fill opacity=0.25] fill between [of=1 and 2,soft clip={domain=0:3.1415}];

\end{axis}
\end{tikzpicture} 

\caption{Integrating $\sin(x)/x$ from $0 \le x < \infty$.}
\label{1}

\end{figure}

\end{document}

我的程式碼位於較大的文件中,因此輸出如下:

在此輸入影像描述

當我取消註釋該行以填充該區域時,會發生這種情況:

在此輸入影像描述

存在明顯的垂直間隙,並且影像被推向左側。知道為什麼會發生這種情況嗎?

答案1

罪魁禍首是scale=1.5,你應該將其傳遞給axis,而不是傳遞給tikzpicture。我猜測否則你會感到困惑pgfplots,請參閱這個答案。如果你想得到核心層面的解釋,恐怕我無法提供,但鑑於這個答案我的第一個猜測是改變 的位置scale,這似乎有效。另請注意,pgfplots 庫fillbetween加載了intersections,但它是一個稍微高級的版本,因此您不應該intersections(再次)加載。我還刪除了tkz-euclide未使用的庫(為了保持程式碼整潔),但您可以重新啟動它們,並且程式碼仍然可以正常工作。

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
%\usepackage{tkz-euclide}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{%polar,colormaps,
fillbetween}
% \usetikzlibrary{arrows,shadows,positioning, calc, decorations.markings, hobby, quotes,angles,decorations.pathreplacing}
% \usepgflibrary{shapes.geometric}
%\usepackage{xcolor}


\begin{document} 

\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]
\begin{axis}[scale=1.5,
color= black,
xmin=-1, 
xmax=7.9, 
ymin=-1, 
ymax=2, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
%xticklabels={}, 
%yticklabels={},
inner axis line style={stealth-stealth},
xlabel = {$x$},
ylabel = {$f(x)$},
ticks=none,
]
\addplot[black, opacity = 0, domain=0:10, name path = 1]{0};

\addplot[Tan, smooth, domain=0.01:7.5, samples = 300, name path=2, thick] {sin(deg(x))/x}; 

\addplot[Tan, fill opacity=0.25] fill between [of=1 and 2,soft clip={domain=0:3.1415}];

\end{axis}
\end{tikzpicture} 
\caption{Integrating $\sin(x)/x$ from $0 \le x < \infty$.}
\label{1}
\end{figure}
\end{document}

在此輸入影像描述

相關內容