[Tex/LaTex] Independent control of node fontsize in pgfplot

fontsizelegendnodespgfplots

I am making several figures in my paper using pgfplot. I want to independently change the fontsize of tick labels, axis labels, legend and some other notes/labels in the figure. Say, 8 pt for axis labels, 6 pt for tick labels, 6 pt for legends, 5 pt for other nodes. see the MWE below,

enter image description here

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{amsmath}
\pgfplotsset{compat=newest}

\pgfplotsset{
    tick label style = {font = {\fontsize{6 pt}{12 pt}\selectfont}},
    label style = {font = {\fontsize{8 pt}{12 pt}\selectfont}},
    legend style = {font = {\fontsize{8 pt}{12 pt}\selectfont}},  
  }

\begin{document}
    \begin{tikzpicture}  
        \begin{axis}[
            width = 6  cm,
            height = 5 cm,
            xmin = 0,
            xmax = 5,
            xlabel = {$x$}, ylabel = {$y$},
            legend entries = {Analytical, Numerical},
            legend pos = north west,       
            ]
            \addplot[solid, black, samples = 100, domain = 0:5] {x^2} node[pos = 0.6,below right] (A) {$x^2$};
            \addplot[only marks, mark = square, black, samples = 20] {x^2};
            \addplot[solid, black, samples = 100, domain = 0:5] {2*x^2} node[pos = 0.75, above left] (B) {$2 x^2$};
        \end{axis}
    \end{tikzpicture}
\end{document}

The fontsize in nodes (A) (2x^2) and (B) (x^2), by default, are too big for me. I know that I can change the fontsize of every node when it is defined. I am trying to find some global way because I have a lot of them in other figures.

I tried

\tikzstyle{every node} = [font = {\fontsize{5 pt}{12 pt}\selectfont}]

It leads to

enter image description here

The fontsize of ticklabels and axis labels stay same, but the fontsize of legend changes (to be same with the nodes). I guess this means that in pgfplots, legend is essentially defined as node.

So my question is that is there any global way to independently change the fontsize of the nodes in pgfplots, without affecting the legend ?

Best Answer

If you want to change the fontsize of nodes inserted by an \addplot command you can use something like

every axis plot/.append style = {font = \scriptsize}

enter image description here

Code:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\pgfplotsset{compat=newest}

\pgfplotsset{
    tick label style = {font = \tiny},
    label style = {font = \small},
    legend style = {font = \footnotesize},
    every axis plot/.append style = {font = \scriptsize}
  }

\begin{document}
    \begin{tikzpicture}  
        \begin{axis}[
            width = 6  cm,
            height = 5 cm,
            xmin = 0,
            xmax = 5,
            xlabel = {$x$}, ylabel = {$y$},
            legend entries = {Analytical, Numerical},
            legend pos = north west,       
            ]
            \addplot[solid, black, samples = 100, domain = 0:5] {x^2} node[pos = 0.6,below right] (A) {$x^2$};
            \addplot[only marks, mark = square, black, samples = 20] {x^2};
            \addplot[solid, black, samples = 100, domain = 0:5] {2*x^2} node[pos = 0.75, above left] (B) {$2 x^2$};
        \end{axis}
    \end{tikzpicture}
\end{document}