问题是,如何将标签垂直对齐到 y 轴中间。上面的代码似乎先旋转标签,然后将其移动到上方。完整的示例粘贴在下面。
有问题的代码是:
\node[rotate=90, above=1] at (y axis mid) {long label};
\begin{tikzpicture}[scale=0.6]
\draw[help lines] (0,0) grid[xstep=1.5,ystep=1] (6,6);
% Axis
\draw[->,>=latex'] (0,0) -- coordinate (x axis mid) (6.5,0);
\draw[->,>=latex'] (0,0) -- coordinate (y axis mid) (0,6.5);
% Labels
\node[below=0.2] at (x axis mid) {long label};
\node[rotate=90,above=1] at (y axis mid) {long label};
\draw[red,very thick] (0,0) -- (0,3) -- (3,3) -- (3,6) -- (6,6);
\end{tikzpicture}
答案1
最简单的方法是在绘制轴时放置节点:
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\draw[help lines] (0,0) grid[xstep=1.5,ystep=1] (6,6);
\draw[->,>=latex'] (0,0) -- node[pos=0.5,below,rotate=0]{long label} (6.5,0);
\draw[->,>=latex'] (0,0) -- node[pos=0.5,above,rotate=90]{long label} (0,6.5);
\draw[red,very thick] (0,0) -- (0,3) -- (3,3) -- (3,6) -- (6,6);
\end{tikzpicture}
\end{document}
pos=<dimen>
您可以分别通过和控制间距和距离(轴和标签之间的)above/below=<dimen>
。
答案2
单独使用above
。这里yshift==xx
也很好用。
代码
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{fit,calc,arrows,positioning}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\draw[help lines] (0,0) grid[xstep=1.5,ystep=1] (6,6);
% Axis
\draw[->,>=latex'] (0,0) -- coordinate (x axis mid) (6.5,0);
\draw[->,>=latex'] (0,0) -- coordinate (y axis mid) (0,6.5);
% Labels
\node[below=0.2] at (x axis mid) {long label};
\node[rotate=90,yshift=10pt] at (y axis mid) {long label}; % Or use above alone.
\draw[red,very thick] (0,0) -- (0,3) -- (3,3) -- (3,6) -- (6,6);
\end{tikzpicture}
\end{document}