使 y 轴在每个刻度处跳转 2

使 y 轴在每个刻度处跳转 2

我正在尝试制作一个图表,我的问题是我想让 y 轴标记从 -2、0、2、4、6、8、10、12.... 开始。但我无法成功。

这是我的代码。

\documentclass[tikz, border=1cm]{standalone}

\usetikzlibrary{arrows,tikzmark,patterns}

\usetikzlibrary{hobby}

\usetikzlibrary{decorations.pathreplacing,calligraphy}

\begin{document}
        % Area under y=-2x+10
\definecolor{gitter}{rgb}{0.65,0.65,0.65} 

\begin{tikzpicture}[line cap=round,line join=round, scale=0.8]  
    
\draw [color=gitter] (-4,-2) grid (6,10);  % Grid område
    
    %Akserne
    \draw[->,thick,>=triangle 45] (-4,0) -- (6,0) node[right] {(1)};
    \draw[->,thick,>=triangle 45] (0,-1) -- (0,10) node[above] {(2)};
    
    %Kurven 
    \draw[blue, very thick] (-4,18/2) -- (6,-1);
    \draw (-2.8,8.4) node {$f$};
        
    % Tekst og pil til området
    \draw (5.5,3.3) node {$A(4)$};
    \draw[->,thick] (5.2,3.1) to[curve through ={(4.7,2.6)}] (4.05,2.1);
    
    \foreach \x/\xtext in {-4,-3,-2,-1, 1,2,3,4,5,6}
    \draw[shift={(\x,0)}, thick] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
    
    \foreach \y/\ytext in {-1,1,2,3,4,5,6,7,8,9}
    \draw[shift={(0,\y)},thick] (2pt,0pt) -- (-2pt,0pt); %node[left] {$\ytext$};
    
\end{tikzpicture}
\end{document}

希望你能帮助我。Jakob

答案1

\foreach \y/\ytext in {-1,0,1,2,3,4,5,6,7,8,9}将迭代 y 刻度位置。将{\the\numexpr\y*2}y 刻度标签设置为位置值的两倍。

\documentclass[tikz, border=1cm]{standalone}

\usetikzlibrary{arrows,tikzmark,patterns}

\usetikzlibrary{hobby}

\usetikzlibrary{decorations.pathreplacing,calligraphy}

\begin{document}
        % Area under y=-2x+10
\definecolor{gitter}{rgb}{0.65,0.65,0.65} 

\begin{tikzpicture}[line cap=round,line join=round, scale=0.8]  
    
\draw [color=gitter] (-4,-2) grid (6,10);  % Grid område
    
    %Akserne
    \draw[->,thick,>=triangle 45] (-4,0) -- (6,0) node[right] {(1)};
    \draw[->,thick,>=triangle 45] (0,-2) -- (0,10) node[above] {(2)};
    
    %Kurven 
    \draw[blue, very thick] (-4,18/2) -- (6,-1);
    \draw (-2.8,8.4) node {$f$};
        
    % Tekst og pil til området
    \draw (5.5,3.3) node {$A(4)$};
    \draw[->,thick] (5.2,3.1) to[curve through ={(4.7,2.6)}] (4.05,2.1);
    
    \foreach \x/\xtext in {-4,-3,-2,-1, 1,2,3,4,5,6}
    \draw[shift={(\x,0)}, thick] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
    
    \foreach \y/\ytext in {-1,0,1,2,3,4,5,6,7,8,9}
    \draw[shift={(0,\y)},thick] (2pt,0pt) -- (-2pt,0pt) node[left] {\the\numexpr\y*2};
    
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容