我有一个 11 年级物理课的大项目。我刚开始使用 latex,我想知道如何绘制图形,但如图所示,x 轴位于图形中间,而我在网上看到的许多 x 轴位于图形的两边。有人能为我提供所示图片的确切代码吗?
如果有人能帮我提供虚线网格线吗?谢谢!
答案1
LaTeX 有许多不同的绘图包,最受欢迎的可能是 PGFPlots 和pst-plot
。它们各有优缺点,因此很难给出一般性建议。对于像这样的简单绘图,任何绘图包都可以。
下面是使用 PGFPlots 创建类似于您在问题中显示的图表的示例:
\documentclass[12pt]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis y line=left, % The y axis should be on the left border
axis x line=middle, % The x axis should go through the origin
grid=both, % Switch on the grid lines
ytick={-9,-6,...,9}, % Manually specify the tick spacing. By default, PGFPlots will try to use multiples of 1, 2, or 5
enlarge y limits=true, % Extend the y axis a little bit
xlabel=$t / \si{\s}$, % Set the x axis label
xlabel style={anchor=south west},
ylabel=$v_y / \si{\m\per\s}$, % Set the y axis label
ylabel style={ % Change the position and rotation of the y axis label
at={(current axis.above origin)},
rotate=-90,
anchor=south west
}
]
\addplot [thick, black] table {
t v
0 0
1 -9
1.25 9
3.25 -9
3.5 9
5.5 -9
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
一个pstricks
解决方案。加载auto-pst-pdf
允许pdfLaTeX
使用--shell-escape
开关(TeX Live、MacTeX)或--enable-write18
(MiKTeX)进行编译:
\documentclass[a4paper,11pt,x11names]{article}
\usepackage{pst-plot}
\usepackage{auto-pst-pdf}
\begin{document}%
\savedata{\mydata}[{{0, 0}, {1, -9}, {1.3, 9}, {3.3 ,-9}, {3.5, 9}, {5.5, -9}]
\footnotesize
\psset{unit = 0.9}
\begin{pspicture}[](0, -4.25)(11,4.25)
\psset{gridwidth=1.pt, subgriddiv=1,gridlabels = 0pt, gridcolor = LightSteelBlue1, griddots=8}
\psgrid(0, -4.25)(11.4, 4.25)
\psset{xunit=1.8cm, yunit=0.3cm, ticks=all, ticksize=-2pt 2pt, Dy=3, dy =3, xyDecimals=1}%, labelsep = 0pt
\psaxes{->}(0,0)(0,-11.995)(5.9, 12.6)[\llap{$t$ (s) },-60][$v_y$ (m/s),-140]
\dataplot[plotstyle=lines, linewidth=1.2pt, linecolor=DodgerBlue3]{\mydata}
\end{pspicture}
\end{document}