如何编写一个循环来在一定范围内增加节点位置及其标签?

如何编写一个循环来在一定范围内增加节点位置及其标签?

这是我目前拥有的代码。我正在使用 tikzpicture:

\begin{tikzpicture}
    \draw[->,very thick] (-5, -5) -- (5, -5); %x-axis
    \draw[->,very thick] (-4, -5.5) -- (-4, 6); %y-axis
    \draw [step=0.5cm] (-4.9, -5.9) grid (4.9, 5.9);
    \node[thick] () at (-4,6.2) {$\textbf{y} $};
    \node[thick] () at (5.2,-5) {$ \textbf{x} $};
    \node[very thick, rotate=90] at (-5.3,0) {\LARGE{\textit{Cost}}};
    \node[very thick] at (0,-6.7) {\LARGE{\textit{Months}}};
        \node[very thick] at (-4,-6.1) {$ 0 $};
        \node[very thick] at (-3.5,-6.1) {$ 1 $};
        \node[very thick] at (-3,-6.1) {$ 2 $};
        \node[very thick] at (-2.5,-6.1) {$ 3 $};
        \node[very thick] at (-2,-6.1) {$ 4 $};
        \node[very thick] at (-1.5,-6.1) {$ 5 $};
        \node[very thick] at (-1,-6.1) {$ 6 $};
        \node[very thick] at (-.5,-6.1) {$ 7 $};
        \node[very thick] at (0,-6.1) {$ 8 $};
        \node[very thick] at (.5,-6.1) {$ 9 $};
        \node[very thick] at (1,-6.1) {$ 10 $};
        \node[very thick] at (1.5,-6.1) {$ 11 $};
        \node[very thick] at (2,-6.1) {$ 12 $};
        \node[very thick] at (2.5,-6.1) {$ 13 $};
            \node[very thick] at (-5.3,-5) {$ 0 $};
            \node[very thick] at (-5.3,-4.5) {$ 5 $};
            \node[very thick] at (-5.3,-4) {$ 10 $};
            \node[very thick] at (-5.3,-3.5) {$ 15 $};
            \node[very thick] at (-5.3,-3) {$ 20 $};
            \node[very thick] at (-5.3,-2.5) {$ 25 $};
            \node[very thick] at (-5.3,-2) {$ 30 $};
    \filldraw (-4,-4) circle (0.1cm);
    \draw[<->,very thick] (-4,-4) -- (4.5,4.5);%steps are 0.5cm, so path is (-4,-4) to (4,4)
    \end{tikzpicture}

答案1

我不完全确定你在问什么,但我猜这涵盖了它。

编辑回答 OP 的问题,语法如下。考虑\foreach \y in {0,5,...,30} \node at (-5.3,{-5+0.1*\y}) {$ \y $}; 一下这意味着创建一个变量\y并使其以 5 为步长从 0 运行到 30。-5+0.1*\y业务是节点的 y 坐标,因此第一个位于 -5。第二个位于 -4.5,等等。

\documentclass{article}

\usepackage{tikz}


\begin{document}
\begin{tikzpicture}
    \draw[->,very thick] (-5, -5) -- (5, -5); %x-axis
    \draw[->,very thick] (-4, -5.5) -- (-4, 6); %y-axis
    \draw [step=0.5cm] (-4.9, -5.9) grid (4.9, 5.9);
    \node[thick] () at (-4,6.2) {$\textbf{y} $};
    \node[thick] () at (5.2,-5) {$ \textbf{x} $};
    \node[very thick, rotate=90] at (-5.3,0) {\LARGE{\textit{Cost}}};
    \node[very thick] at (0,-6.7) {\LARGE{\textit{Months}}};
     \foreach \y in {0,5,...,30}
    \node at (-5.3,{-5+0.1*\y}) {$ \y $};
    \foreach \x in {0,...,13}
        \node[very thick] at ({-4+0.5*\x},-6.1) {$ \x $};
    \filldraw (-4,-4) circle (0.1cm);
    \draw[<->,very thick] (-4,-4) -- (4.5,4.5);%steps are 0.5cm, so path is (-4,-4) to (4,4)
    \end{tikzpicture}

\end{document}

答案2

@JPi 答案的一个小变化(+1)...

编辑: 我看不出有任何合理的理由从(-5, -5)(5, -5)和 从 到(-4, -5.5)绘制轴(-4, 6),然后指向(-4,-4)指定为图表轴原点。从 的原点绘制轴要容易得多(0,0)

因此,我的方法和您的 MWE 的区别在于确定所有图像元素的坐标。此外,我删除了所有不需要的节点选项,并且x节点y样式使用与 更一致的语法tikz

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}

    \begin{tikzpicture}
\draw[->,very thick] (-0.8,0) -- node[below=12mm,font=\LARGE\itshape] {Months}
                  ++ ( 9.8,0) node[right] {$\mathbf{x}$};%x-axis
\draw[->,very thick] (0,-0.8) -- node[above=9mm,sloped,font=\LARGE\itshape] {Cost}
                  ++ (0,10.8) node[above] {$\mathbf{y}$};%y-axis
\draw [step=5mm] (-0.9,-0.9) grid (8.9, 9.9);
%
\foreach \i [count=\ii] in {0,0.5,...,6}
    \node[below] at (\i,-0.9) {\ii};
\foreach \i in {0,5,...,30}
    \node[left] at (-0.9,\i/10) {\i};
\filldraw (0,1) circle (1mm);
\draw[->,very thick] (0,1) -- (8.5,9.5);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容