使用 pgfplots 和 LaTeX 绘制简单绘图
我刚刚花了一个小时寻找如何在 LaTeX 中绘制一个简单的函数,例如 f(x)=x^2。我尝试过的例子由于某种未知的原因不起作用,很可能是因为 pdflatex 不会启动 gnuplot (-enable-xxx 选项不起作用),但我发现了一个可以工作的 LaTeX 包:pgfplots。唯一的问题是文档不包含我需要的简单示例。是的,如果我想用什么东西绘制复杂的图表,那么我可能会找到一些东西,但是一个非常简单的函数示例——不走运!但也许我只需要新眼镜。所以,我很高兴能得到一个 LaTeX 函数图的简单示例,其中包含 x 轴和 y 轴以及图表!
编辑:我发现这是迄今为止最好的解决方案,但我还不满意,例如 x 标签位于中间。我真的只想要一个标准的绘图,没有任何花哨的颜色或 3d 或类似的东西。
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
xlabel=$x$,
ylabel={$x^2$},
xmin=-4, xmax=4,
ymin=0, ymax=4,
width=0.8\textwidth,
height=5cm
]
\addplot[mark=noner,smooth,domain=-1:1]{x^2};
\end{axis}
\end{tikzpicture}
I've just been searching like an hour on how to draw a simple function in LaTeX, for example f(x)=x^2. The examples I've tried did not work for some unknown reason, most probably because pdflatex would not start gnuplot (the -enable-xxx options do not work), but I found a LaTeX package that works: pgfplots. The only problem is that the documentation does not contain a simple example like what I need. Yes, if I wanted to draw complex diagrams with whatnot then I might find something, but a really simple example of a function -- no luck! But maybe I just need new glasses. So please, I'd be glad to get a simple example of a function plot in LaTeX, with an x and y axis and a graph!
EDIT: I've found this the best solution so far, but I'm not yet satisfied, for example the x label is in the middle. I really just want a standard plot without any fancy colors or 3d or similar.
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
xlabel=$x$,
ylabel={$x^2$},
xmin=-4, xmax=4,
ymin=0, ymax=4,
width=0.8\textwidth,
height=5cm
]
\addplot[mark=noner,smooth,domain=-1:1]{x^2};
\end{axis}
\end{tikzpicture}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于简单的函数,您不需要 Gnuplot,
pgfplots
能够在 LaTeX 中进行计算。请参阅 pgfplots 手册第 4.2.4 节中的示例;第一个示例绘制函数 x ↦ x² − 4。For simple functions you don't need Gnuplot,
pgfplots
is able to do the calculations in LaTeX. See the examples in section 4.2.4 of thepgfplots
manual; the first example draws the function x ↦ x² − 4.