如何调整 TikZ 中的轴标签?(没有 pgfplot)

如何调整 TikZ 中的轴标签?(没有 pgfplot)

我希望 x 值的标签为 0、0.1、0.2、0.3,然后增加到 1.6(即 8cm)。但是我对 -method 理解不够\xtext。有人能指出如何使用 -command 自动放置标签吗\foreach

平均能量损失

\documentclass{scrartcl}

\usepackage{tikz,relsize,amsmath}
\usetikzlibrary{calc,trees,shadows,positioning,arrows,chains,shapes.geometric,shapes,matrix,shapes.symbols,patterns}

\usepackage[T1]{fontenc}
\usepackage{tgheros}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %Netz
    \draw[style=help lines,step=0.5cm] (0,0) grid (8.1,10.1);
    %Achsentitel (Legende)
    \draw[->,thick] (-0.1,0) -- (8.4,0) node[anchor=west] {\(\frac{\text{x}_{\text{1}}}{\text{y}}\)}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,10.4) node[anchor=east] {\(\frac{\text{x}_{\text{2}}}{\text{y}}\)}; %Y-Achse
    %ticks an Achsen
    \foreach \x in {0,1,...,9} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,10} \draw [thick](-2pt,\y) -- (2pt,\y);
    %Achsenbeschriftung (Zahlen an den Achsen)
    \foreach \x/\xtext in {0/0,1/0.2,...,8/1.6} \draw (\x cm, 0 cm) node[anchor=north] {\xtext};
    %\foreach \y in {0,1,...,10} \draw (0 cm, \y cm) node[anchor=east]{\y};
    %Stores
    \filldraw (4,7.5) circle (0.08cm) node[anchor=north west] {A};
\end{tikzpicture}
\end{center}
\end{document}

答案1

这是一种可能性;标签是位置的五分之一,因此您可以使用语法evaluate=<variable> as <macro> using <formula>\pgfmathroundto用于对数字进行四舍五入):

\documentclass{scrartcl}

\usepackage{tikz,relsize,amsmath}
\usetikzlibrary{calc,trees,shadows,positioning,arrows,chains,shapes.geometric,shapes,matrix,shapes.symbols,patterns}

\usepackage[T1]{fontenc}
\usepackage{tgheros}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
    %Netz
    \draw[style=help lines,step=0.5cm] (0,0) grid (8.1,10.1);
    %Achsentitel (Legende)
    \draw[->,thick] (-0.1,0) -- (8.4,0) node[anchor=west] {\(\frac{\text{x}_{\text{1}}}{\text{y}}\)}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,10.4) node[anchor=east] {\(\frac{\text{x}_{\text{2}}}{\text{y}}\)}; %Y-Achse
    %ticks an Achsen
    \foreach \x in {0,1,...,9} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
    \foreach \y in {0,1,...,10} \draw [thick](-2pt,\y) -- (2pt,\y);
    %Achsenbeschriftung (Zahlen an den Achsen)
    \foreach \x [evaluate=\x as \xval using (1/5)*\x] in {0,1,...,8} 
  \draw (\x cm, 0 cm) node[anchor=north] {\pgfmathroundto{\xval}\pgfmathresult};
    %\foreach \y in {0,1,...,10} \draw (0 cm, \y cm) node[anchor=east]{\y};
    %Stores
    \filldraw (4,7.5) circle (0.08cm) node[anchor=north west] {A};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容