大家好,
我正在为学校学生制作图表,我正在为学校项目制作数字线。请注意,我仅使用 LaTeX 来创建图表,因此我下载了 .png 文件并将其用于我的内容。在创建数字线时,当线的范围更广时,即 -40{to}40 和 -60{to}60,我遇到了一个问题。当范围相对较小时,这是一项简单的任务,但对于较大的范围,标签会相互重叠。
这个问题有解决办法吗?我正在分享我从这个伟大的社区学到的工作。
\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots} \usepackage{pgf,tikz,pgfplots,xcolor}
\pgfplotsset{compat=1.9}
\pgfplotsset{%
compat=newest,
tick label style={font=\tiny},
label style={font=\small},
legend style={font=\small},
axis x line = center,
axis y line = center,
every axis/.style={pin distance=1ex},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis x line=center,
axis y line=none,
inner axis line style={<->},
xmin=-60,xmax=60, ymin=0, ymax=1,
xtick distance=5,
%xticklabels={-60,-55,-50,-45,-40,-35,-30,-25,-20,-15,-10,-5,0,5,10,15,20,25,30,35,40,45,50,55,60},
]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[%
axis x line=center,
axis y line=none,
inner axis line style={<->},
ymin=0,ymax=1,
xmin=-43,xmax=43,
xtick distance = 5,
axis line style = thick,
%major tick style = thick,
%i am using the following if necessary
%x tick label style = {font=\tiny}
%xtick={1,2,3},
%xtick style={},
%xtick={-4,-3,-2,-1,1,2,3,4},
]
\end{axis}
\end{tikzpicture}
\end{document}
答案1
你TikZ
可以用它绘制自己的数字线。我创建了一个相应的命令供你使用。我鼓励你尝试不同的参数,以便在每种情况下都能得到你想要的结果。
\documentclass[tikz,border=3.14pt]{standalone}
\newcommand\drawNumberLine[4]{%
\begin{tikzpicture}
% draw arrow from -arrowLength/2-0.5 to arrowLength/2+0.5
\draw[latex-latex,thick] (-#1/2 -.5,0) -- (#1/2 + .5,0);
% draw ticks with label
\pgfmathtruncatemacro{\nextStep}{#2+#3}
\foreach \x [count=\c] in {#2,\nextStep,...,#4} {
% scale \x to arrow length
% (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
\pgfmathsetmacro{\xPos}{(\x - #2) * (#1/2 - -#1/2) / (#4 - #2) + -#1/2};
% draw tick
\draw[thick] (\xPos,-.1) -- (\xPos,.1);
% tick label above or below? (based on \c which starts at 1)
% - even: below
% - odd: above
\pgfmathsetmacro{\yPos}{ifthenelse(mod(\c,2)==0,-.3,.3)}
% \x < 0: shift a little bit to the left (-.12)
\pgfmathsetmacro{\xPos}{ifthenelse(\x<0,\xPos-.12,\xPos)}
% set tick label
\node[font=\footnotesize] at (\xPos,\yPos) {$\x$};
}
\end{tikzpicture}
}
% here are the labels always below
\newcommand\drawNumberLineLabelsBelow[4]{%
\begin{tikzpicture}
% draw arrow from -arrowLength/2-0.5 to arrowLength/2+0.5
\draw[latex-latex,thick] (-#1/2 -.5,0) -- (#1/2 + .5,0);
% draw ticks with label
\pgfmathtruncatemacro{\nextStep}{#2+#3}
\foreach \x [count=\c] in {#2,\nextStep,...,#4} {
% scale \x to arrow length
% (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
\pgfmathsetmacro{\xPos}{(\x - #2) * (#1/2 - -#1/2) / (#4 - #2) + -#1/2};
% draw tick
\draw[thick] (\xPos,-.1) -- (\xPos,.1);
% tick label position
\pgfmathsetmacro{\yPos}{-.3}
% \x < 0: shift a little bit to the left (-.12)
\pgfmathsetmacro{\xPos}{ifthenelse(\x<0,\xPos-.12,\xPos)}
% set tick label
\node[font=\footnotesize] at (\xPos,\yPos) {$\x$};
}
\end{tikzpicture}
}
\begin{document}
\drawNumberLine%
{8}% arrow length
{-40}% start point
{5}% step wide
{40}% end point
\drawNumberLine%
{8}% arrow length
{-60}% start point
{5}% step wide
{60}% end point
\drawNumberLine%
{8}% arrow length
{-20}% start point
{2}% step wide
{20}% end point
\drawNumberLine%
{8}% arrow length
{15}% start point
{5}% step wide
{95}% end point
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\drawNumberLineLabelsBelow%
{15}% arrow length
{-40}% start point
{5}% step wide
{40}% end point
\drawNumberLineLabelsBelow%
{15}% arrow length
{-60}% start point
{5}% step wide
{60}% end point
\drawNumberLineLabelsBelow%
{15}% arrow length
{-20}% start point
{2}% step wide
{20}% end point
\drawNumberLineLabelsBelow%
{15}% arrow length
{15}% start point
{5}% step wide
{95}% end point
\end{document}
一些结果:
标签在上方和下方之间变化的一些结果:
新的:15 至 95