我怎样才能将数字放入地块的标记中?

我怎样才能将数字放入地块的标记中?

我有以下情节

在此处输入图片描述

使用这个乳胶源:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{plotmarks}

%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

% The data files, written on the first run.
\begin{filecontents}{function.data}
# n     m
1 1
1 2
2 1
1 3
2 2
3 1
1 4
2 3
3 2
4 1
1 5
2 4
3 3
4 2
5 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}[y=.5cm, x=.5cm,font=\sffamily]
    %axis
    \draw (0,0) -- coordinate (x axis mid) (5,0);
        \draw (0,0) -- coordinate (y axis mid) (0,5);
        %ticks
        \foreach \x in {0,...,5}
            \draw (\x,1pt) -- (\x,-3pt)
            node[anchor=north] {\x};
        \foreach \y in {0,...,5}
            \draw (1pt,\y) -- (-3pt,\y) 
                node[anchor=east] {\y}; 
    %labels      
    \node[below=0.8cm] at (x axis mid) {n};
    \node[rotate=90, above=0.8cm] at (y axis mid) {m};

    %plots
    \draw plot[mark=square*]
        file {function.data};  

    %legend
    \begin{scope}[shift={(4,4)}] 
    \draw (0,0) -- 
        plot[mark=square*, mark options={fill=black}] (0.25,0) -- (0.5,0)
        node[right]{f(m, n)};
    \end{scope}
\end{tikzpicture}
\end{document}

现在我想在标记中放入数字或用数字替换标记。这怎么可能呢?

答案1

我会使用 PGFplots 来实现这一点,而不是手动绘制图表。在 PGFplots 中,您可以使用键nodes near coords在每个数据点生成标签。

这是您在 PGFplots 中重现的图,带有标签。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{sansmath} % for sans serif math

%%%<
% The data files, written on the first run.
\begin{filecontents}{function.data}
# n     m
1 1
1 2
2 1
1 3
2 2
3 1
1 4
2 3
3 2
4 1
1 5
2 4
3 3
4 2
5 1
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    compat=newest, % for better label placement
    font=\sansmath\sffamily, % math and normal text in sans serif
    xlabel=n, ylabel=m, % the label texts
    xmin=0, ymin=0, % axis origin
    enlarge y limits=false, % don't enlarge the y axis beyond the data range
    enlarge x limits={upper,abs=0.02}, % enlarge x axis slightly to make sure the last tick mark is drawn completely
    axis lines*=left, % only draw the left axis lines, not a box
    unit vector ratio*={1 1 1}, % equal axis scaling. "*" to make sure the axes can only be reduced in size, not enlarged
    width=6cm, % set the overall width of the plot
    try min ticks=5, % adjusts how many ticks are printed
    tick align=center, % tick marks centered on the axes
    legend style={
        draw=none, % no frame around axes
        at={(1,1)}, % place at upper right of plot
        anchor=north % use upper middle edge of legend for alignment
    },
]
\addplot [
    mark=square*, mark size=0.5em, % square, filled ("*"), radius of 0.5em
    nodes near coords={
        \pgfmathparse{int(\coordindex+1)}
        \pgfmathresult
    }, % print labels on each data point, using `\coordindex` (the data point counter) increased by 1
    every node near coord/.style={
        font=\scriptsize\sffamily\bfseries, % smaller text size, bold for the data point labels
        text=white,
        anchor=center % center the labels on the plot marks
    }
    ] table {function.data};
\addlegendentry{f(m,n)}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容