![如何绘制速度时间图的负 y 轴?](https://linux22.com/image/457974/%E5%A6%82%E4%BD%95%E7%BB%98%E5%88%B6%E9%80%9F%E5%BA%A6%E6%97%B6%E9%97%B4%E5%9B%BE%E7%9A%84%E8%B4%9F%20y%20%E8%BD%B4%EF%BC%9F.png)
我有这个速度-时间图,但我不知道如何绘制带有刻度(-1,-2)的负$y$轴。
这是代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{physics}
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\colorlet{myblue}{blue!70!black}
\colorlet{mydarkblue}{blue!40!black}
\colorlet{mygreen}{green!60!black}
\colorlet{myred}{red!65!black}
\colorlet{mypurple}{red!50!blue!95!black!75}
\tikzstyle{wave}=[myblue,thick]
\tikzstyle{xline}=[very thick,myblue]
\tikzstyle{vline}=[very thick,mygreen]
\tikzstyle{aline}=[very thick,mypurple]
\tikzstyle{mydashed}=[mydarkblue,dashed]
\def\tick#1#2{\draw[thick] (#1) ++ (#2:0.1) --++ (#2-180:0.2)}
\def\tlabel{$t\,\left[\si{s}\right]$}
\def\vlabel{$v\,\left[\si{m/s}\right]$}
\def\alabel{$a\,\left[\si{m/s^2}\right]$}
\begin{document}
% VELOCITY
\def\xmax{6} % maximum x axis (time)
\def\ymax{2.0} % maximum y axis (velocity)
\def\tmax{180} % maximum time on x axis
\begin{tikzpicture}
\def\vmax{3} % maximum velocity on x axis
\def\xscale{\xmax/\tmax}
\def\yscale{\ymax/\vmax}
% GRID
\foreach \t in {0,20,...,\tmax}{
\draw[black!80,dotted] (\t*\xscale,0) --++ (0,\vmax*\yscale+1);
\tick{\t*\xscale,0}{90} node[below=-1,scale=0.77] {\t};
}
\foreach \v in {0,2,...,\vmax}{
\draw[black!80,dotted] (0,\v*\yscale) --++ (\tmax*\xscale+0.3,0);
\tick{0,\v*\yscale}{0} node[left=-1,scale=0.77] {\v};
}
% AXES
\draw[->,thick]
(0,0) -- (\xmax+1,0) node[midway,below=9] {\tlabel};
\draw[->,thick]
(0,0) -- (0,\ymax+1) node[midway,rotate=90,above=9] {\vlabel};
% GRAPH
\draw[vline,line cap=round,xscale=\xscale,yscale=\yscale]
(0,0) -- (40,2) -- (60,2) -- (160,-2);
\end{tikzpicture}
\end{document}
答案1
如果pgfplots
没有这个选项,我会使用命令grid
及其相关help lines
样式来绘制网格。它的使用非常简单:
\draw [help lines] (<x1>, <y1>) grid (<x2>, <y2>);
此外,正如您所定义的xscale
,yscale
我建议将这些选项作为整体的参数tikzfigure
,如下所示:
\begin{tikzpicture}[xscale=\xscale,yscale=\yscale,% etc...
这样您就可以使用t
- 和v
-坐标,而不是绝对几何 x 和 y 坐标。
最后,我稍微重新整理了一下代码,以便我认为它更清晰一些。
\documentclass{article}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{physics}
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\usetikzlibrary{backgrounds}
\colorlet{myblue}{blue!70!black}
\colorlet{mydarkblue}{blue!40!black}
\colorlet{mygreen}{green!60!black}
\colorlet{myred}{red!65!black}
\colorlet{mypurple}{red!50!blue!95!black!75}
\tikzstyle{wave}=[myblue,thick]
\tikzstyle{xline}=[very thick,myblue]
\tikzstyle{vline}=[very thick,mygreen]
\tikzstyle{aline}=[very thick,mypurple]
\tikzstyle{mydashed}=[mydarkblue,dashed]
\def\tick#1#2{\draw[thick] (#1) ++ (#2:0.1) --++ (#2-180:0.2)}
\def\tlabel{$t\,\left[\si{s}\right]$}
\def\vlabel{$v\,\left[\si{m/s}\right]$}
\def\alabel{$a\,\left[\si{m/s^2}\right]$}
\begin{document}
% VELOCITY
\def\xmax{6} % maximum x axis (time)
\def\ymax{2.0} % maximum y axis (velocity)
\def\tmax{180} % maximum time on x axis
\def\vmax{3} % maximum velocity on y axis
\def\tstep{20} % step of ticks in t-axis
\def\vstep{1} % step of ticks in v-axis
\def\xscale{\xmax/\tmax}
\def\yscale{\ymax/\vmax}
\begin{tikzpicture}[xscale=\xscale,yscale=\yscale,
help lines/.style={black!80,dotted,ystep=1,xstep=20}]
% GRID
\draw [help lines,on background layer] (-\tstep,-2*\vstep) grid (\tmax,\vmax);
% TICKS
\foreach \t in {-\tstep,0,...,\tmax}{
\tick{\t,0}{90} node[below=-1,scale=0.77] {\t};
}
\pgfmathtruncatemacro{\vtickmax}{\vmax+2*\vstep}
\foreach \v [evaluate=\v as \vv using int(\v-2*\vstep)] in {0,\vstep,...,\vtickmax}{
\tick{0,\v-2*\vstep}{0} node[left=-1,scale=0.77] {\vv};
}
% AXES
\draw[->,thick] (-2*\tstep,0) -- (\tmax+\tstep,0) node[right] {\tlabel};
\draw[->,thick] (0,-3*\vstep) -- (0,\vmax+\vstep) node[above] {\vlabel};
% CURVE
\draw[vline,line cap=round] (0,0) -- (40,2) -- (60,2) -- (160,-2);
\end{tikzpicture}
\end{document}