Tikz:在 tikzpicture 中声明一个函数弄乱了我的轴

Tikz:在 tikzpicture 中声明一个函数弄乱了我的轴

我正在尝试创建一些图表来说明黎曼和,但在声明函数时无法使轴线居中。注意:我正在使用一些在线找到的代码,这些代码需要声明一个函数才能生成黎曼和近似中的矩形,这也发生在投影仪幻灯片中。

我有以下代码:

\documentclass[slidestop,compress,mathserif,red]{beamer}
\usetheme{Warsaw} 
\usecolortheme{lily} 
\beamersetaveragebackground{brown!30!white}


\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{tikz-cd}
\usepackage{graphics}
\usepackage{pgfplots}


\pgfplotsset{compat=newest}
\usepgfplotslibrary{patchplots}



\pgfplotsset{
    integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
    integral segments=3,
    integral/.style args={#1:#2}{
        ybar interval,
        domain=#1+((#2-#1)/\integralsegments)/2:#2+((#2-#1)/\integralsegments)/2,
        samples=\integralsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}
    }
}



\begin{document} 

\begin{frame}[c]{ }
\frametitle{Section 6.3: Area and the Definite Integral}


\begin{center}
\begin{tikzpicture}
\begin{axis}
%[color=black,/pgf/declare function={f=0.125*x^3-x^2+1.5*x + 3;}]
[
    axis lines=center,
    grid=major,
    xmin=-1.5,
    xmax=5.5,
    ymin=-1.5,
    ymax=4.5,
    extra x ticks = {-2,-1,1,2,3,4,5,6}, 
    extra y ticks = {-1,1,2,3,4},
    no marks,
    axis line style={<->},
]
%\addplot [<->, smooth,black, thick, domain = -1:5] {f};
%\addplot [
%    red,
%    fill=red,
%    opacity=0.4,
%    integral segments=10,
%    integral=0:5
%] {f};
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

其结果如下:一组漂亮的中心轴线然而,当我取消注释绘制函数和黎曼和的代码部分时,我得到了以下输出:轴线不居中

我怎样才能保持轴线居中并保留情节的重要部分?

答案1

说实话,我甚至没有试过你的代码,因为你并没有真正定义一个函数,所以我无法告诉你哪里出了问题。但是,如果你定义了一个具有显式x依赖关系的真正函数,那就没有问题了。

\documentclass[slidestop,compress,mathserif,red]{beamer}
\usetheme{Warsaw} 
\usecolortheme{lily} 
\beamersetaveragebackground{brown!30!white}


\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{tikz-cd}
\usepackage{graphics}
\usepackage{pgfplots}


\pgfplotsset{compat=newest}
\usepgfplotslibrary{patchplots}



\pgfplotsset{
    integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
    integral segments=3,
    integral/.style args={#1:#2}{
        ybar interval,
        domain={#1+((#2-#1)/\integralsegments)/2}:{#2+((#2-#1)/\integralsegments)/2},
        samples=\integralsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}
    }
}



\begin{document} 

\begin{frame}[c]{ }
\frametitle{Section 6.3: Area and the Definite Integral}


\begin{center}
\begin{tikzpicture}
\begin{axis}[color=black,
declare function={f(\x)=0.125*\x*\x*\x-\x*\x+1.5*\x + 3;},
    axis lines=center,
    grid=major,
    xmin=-1.5,
    xmax=5.5,
    ymin=-1.5,
    ymax=4.5,
    extra x ticks = {-2,-1,1,2,3,4,5,6}, 
    extra y ticks = {-1,1,2,3,4},
    no marks,
    axis line style={<->},
]
\addplot [stealth-stealth,smooth, black, thick, domain = -1:5] {f(x)};
\addplot [
   red,
   fill=red,
   opacity=0.4,
   integral segments=10,
   integral=0:5
] {f(x)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

相关内容