![Tikz:在 tikzpicture 中声明一个函数弄乱了我的轴](https://linux22.com/image/412234/Tikz%EF%BC%9A%E5%9C%A8%20tikzpicture%20%E4%B8%AD%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%E5%BC%84%E4%B9%B1%E4%BA%86%E6%88%91%E7%9A%84%E8%BD%B4.png)
我正在尝试创建一些图表来说明黎曼和,但在声明函数时无法使轴线居中。注意:我正在使用一些在线找到的代码,这些代码需要声明一个函数才能生成黎曼和近似中的矩形,这也发生在投影仪幻灯片中。
我有以下代码:
\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}