在 TikZ 图上更改 x 轴上的标签

在 TikZ 图上更改 x 轴上的标签

我有以下代码来创建一个必须绘制三角函数的轴(以度为单位)。它将被打印出来并用作数学试卷的一部分。但是,x轴的步长必须为 30 度。

我如何修改下面的代码x-axis,使我的 上的每个标签都乘以 30?即标签必须是0; 30; 60; 90; 120; ...

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{tikz}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{gensymb}

\begin{document}
\begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = north west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south east] {$y$};
\foreach \x in {1,...,13}
   \draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {$\x \degree$};
\foreach \y in {-3,..., 3}
   \draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我稍微修改了您的\foreach语句以包括[evaluate=\x as \degree using int(\x*30)]$\degree^\circ$打印度数。您可以通过更改数字来更改步骤30

输出

度数

代码

\documentclass[margin=10pt]{standalone}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{gensymb}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = north west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south east] {$y$};

\foreach \x [evaluate=\x as \degree using int(\x*30)] in {1,...,12}{ 
   \draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {$\degree^\circ$};
   }
\foreach \y in {-3,-2,-1,1,2,3}
   \draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}
\end{document}

答案2

你可以使用\pgfmathparse乘法。由于你得到的是小数,你可能int还需要

node[anchor = north] {\pgfmathparse{int(30*\x)}$\SI{\pgfmathresult}{\degree}$};

请注意我已经使用了siunitx度数。

\documentclass{article}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
  \begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = south west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south west] {$y$};
\foreach \x in {1,...,13}
   \draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {\pgfmathparse{int(30*\x)}$\SI{\pgfmathresult}{\degree}$};
\foreach \y in {-3,-2,-1,1,2, 3}
   \draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

使用 MetaPost 执行此操作的方法,可能会引起您的兴趣。主要问题在于选择 x 轴的相关刻度,并允许在此轴的标签内进行循环,这要归功于\mplibtextextlabel{enable}序言中的指令。这使得每个命令的字符串参数label都可以通过textext宏由 (Lua)LaTeX 排版,这比通常的标志灵活得多btex … etex

\documentclass[border=2mm]{standalone}
\usepackage{siunitx}
\usepackage{luamplib}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    numeric u, v, xmin, xmax, xstep, ymin, ymax, ystep; 
    u = cm/30; xmin = -30; xmax = 390; xstep = 15; 
    v = cm; ymax = 4 = -ymin; ystep = .5;
    beginfig(1);
      % Grid
      drawoptions(withcolor .8white);
      for i = ceiling(xmin/xstep) upto floor(xmax/xstep):
        draw ((i*xstep, ymin) -- (i*xstep, ymax)) xscaled u yscaled v;
      endfor
      for j = ceiling(ymin/ystep) upto floor(ymax/ystep):
        draw ((xmin, j*ystep) -- (xmax, j*ystep)) xscaled u yscaled v;
      endfor
      drawoptions(); labeloffset := 5bp;
      % x-axis marks and labels
      for i = 2xstep step 2xstep until xmax-2xstep:
        if i<>0: 
          label.bot("$" & decimal i & "\si\degree$", (i*u, 0)); 
          draw (i*u, -2bp) -- (i*u, 2bp);
        fi
      endfor
      % y-axis marks and labels
      for j = ymin+1 step 2ystep until ymax-1:
        if j<>0:
          draw(-2bp, j*v) -- (2bp, j*v);
          label.lft("$" & decimal j & "$", (0, j*v));
        fi  
      endfor 
      % Axes and other labels
      drawarrow (xmin*u, 0) -- (xmax*u, 0);
      drawarrow (0, ymin*v) -- (0, ymax*v);
      labeloffset := 3bp; label.llft("$O$", origin); 
      label.bot("$x$", (xmax*u, 0)); label.lft("$y$", (0, ymax*v));
    endfig;
  \end{mplibcode}
\end{document}

使用 LuaLaTeX 排版。输出:

在此处输入图片描述

答案4

PSTricks 解决方案:

\documentclass{article}

\usepackage[margin = 3cm]{geometry}
\usepackage{pst-plot}
\usepackage{siunitx}
\usepackage{xfp}

\makeatletter
  \def\pst@@@hlabel#1{\ang{\fpeval{30*#1}}}
\makeatother

\begin{document}

\begin{pspicture*}(-1,-4)(13.36,4.4)
  \psgrid[subgriddiv = 2, gridcolor = lightgray](-1,-4)(12.9,3.9)
  \psaxes{->}(0,0)(-0.99,-3.99)(13,4)[$x$,0][$y$,90]
\end{pspicture*}

\end{document}

输出

相关内容