Tikz:y 轴上刻度之间的间距不等

Tikz:y 轴上刻度之间的间距不等

我正在尝试绘制一个 y 轴每 100 个单位刻度的图表。我使用 tikz。这是代码。

\documentclass[12pt]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[thick,->] (0,2.5) -- (4.5,2.5) node[anchor=north west] {x axis};
\draw[thick,->] (0,2.5) -- (0,6.5) node[anchor=south east] {y axis};
\foreach \y in {300,400,500,600}
    \draw (1pt, 0.01*\y cm) -- (-1pt, 0.01*\y cm) node[anchor=east] {$\y$};
\end{tikzpicture}

\end{document}

问题是:刻度之间的间距对我来说似乎不相等(特别是,500 和 600 看起来比 400 和 500 更接近)。请注意,我还收到一条错误消息:!尺寸太大。

但是,当我不将 \y 除以 100 时,就不会出现这个问题。

        \begin{tikzpicture}
\draw[thick,->] (0,2.5) -- (4.5,2.5) node[anchor=north west] {x axis};
\draw[thick,->] (0,2.5) -- (0,7) node[anchor=south east] {y axis};
\foreach \y in {3,4,5,6}
    \draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor=east] {$\y$};

\end{tikzpicture}

任何想法?

答案1

您可以删除,cm因为的默认单位y1cm

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[thick,->] (0,2.5) -- (4.5,2.5) node[anchor=north west] {x axis};
\draw[thick,->] (0,2.5) -- (0,6.5) node[anchor=south east] {y axis};
\foreach \y in {300,400,500,600}
    \draw (1pt, 0.01*\y) -- (-1pt, 0.01*\y) node[anchor=east] {$\y$};
\end{tikzpicture}
\end{document}

或者在单位前使用数字,例如

\foreach \y in {300,400,500,600}
    \draw (1pt, 0.01cm*\y) -- (-1pt, 0.01cm*\y) node[anchor=east] {$\y$};

或者

\foreach \y in {300,400,500,600}
    \draw (1pt, \y*0.01cm) -- (-1pt, \y*0.01cm) node[anchor=east] {$\y$};

或者

\foreach \y in {300,400,500,600}
    \draw (1pt, 0.01*\y*1cm) -- (-1pt, 0.01*\y*1cm) node[anchor=east] {$\y$};

也可以更改y单个路径的单位

\foreach \y in {300,400,500,600}
    \draw[x=1pt,y=0.01cm] (1, \y) -- (-1, \y) node[anchor=east] {$\y$};

或整个tikzpicture环境:

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y=0.01cm]
\draw[thick,->] (0,250) -- (4.5,250) node[anchor=north west] {x axis};
\draw[thick,->] (0,250) -- (0,650) node[anchor=south east] {y axis};
\foreach \y in {300,400,500,600}
    \draw[x=1pt] (1, \y) -- (-1, \y) node[anchor=east] {$\y$};
\end{tikzpicture}
\end{document}

答案2

我对\foreachs 的一般规则是让变量尽可能简单\foreach,最好是整数,并从中计算更复杂的东西。因此,在您的示例中,我不会迭代标签并计算坐标,而是迭代坐标并计算标签。

\documentclass[12pt]{article}
%\url{https://tex.stackexchange.com/q/373191/86}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[thick,->] (0,2.5) -- (4.5,2.5) node[anchor=north west] {\(x\) axis};
\draw[thick,->] (0,2.5) -- (0,6.5) node[anchor=south east] {\(y\) axis};
\foreach[evaluate=\y as \lbl using int(\y*100)] \y in {3,4,5,6}
    \draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor=east] {$\lbl$};
\end{tikzpicture}

\end{document}

请注意,您还可以使用范围的语法{3,...,6},在这种情况下,这不会给您带来很多好处,但对于更大的范围很有用。

tikz 中均匀分布的刻度

答案3

MWE 的微小变化(刻度之间的距离相等):

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
\draw[thick,->] (0,0) -- + (4.5,0) node[below right] {x axis};
\draw[thick,->] (0,0) -- + (0,6.5) node[above  left] {y axis};
\foreach \y in {300,400,500,600}
    \draw (2pt, \y/100) -- + (-4pt,0) node[left] {$\y$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容