首页 > 解决方案 > 减小条形图中“坐标附近的节点”中的字体大小#tikzpicture #pgfplots

问题描述

我试图在以下代码中减小坐标附近节点中的字体大小,但我发现避免节点编号重叠的唯一解决方案是减少它们的小数位数。

关于如何减少coords 附近节点字体的任何建议?

\newcommand{\figureHeight}{0.5625} %% 16:9
\pgfplotsset{
    compat           = 1.13,
    grid             = major,
    enlarge x limits = 0,
    cycle list name  = tum,
    major grid style = {dotted},
    minor grid style = {dotted},
    width            = \hsize,
    %width            = \hsize * 0.9,
    height           = \hsize * 0.9 * \figureHeight,
    legend style     = { at = {(0.98,0.96)}, anchor = north east,}
}

\begin{figure}[thb]
    \centering

    \begin{tikzpicture}
    \begin{axis}[
    ybar               = 5pt, % configures `bar shift'
    xmajorgrids        = false,
    x tick label style = {
        /pgf/number format/1000 sep =},
    xtick              = { 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018},
    ylabel             = Energy Produced in TWh,
    enlarge x limits   = 0.1,
    ymin               = 0,
    ymax               = 125,
    bar width          = 4pt,
    legend style={at={(0.02,0.96)},anchor=north west},
    nodes near coords
    ]

    %Hidro
    \addplot coordinates { ( 2011 , 18 ) ( 2012 , 21 ) ( 2013 , 23 ) ( 2014 , 19 ) ( 2015 , 19 ) ( 2016 , 21 ) ( 2017 , 20 ) ( 2018 , 19 )};
    \addlegendentry{Hydro}

    %Biomass
    \addplot coordinates { ( 2011 , 34 ) ( 2012 , 40 ) ( 2013 , 41 ) ( 2014 , 43 ) ( 2015 , 44 ) ( 2016 , 45 ) ( 2017 , 45 ) ( 2018 , 45 )};
    \addlegendentry{Biomass}

    %Wind
    \addplot coordinates { ( 2011 , 50 ) ( 2012 , 52 ) ( 2013 , 53 ) ( 2014 , 59 ) ( 2015 , 81 ) ( 2016 , 80 ) ( 2017 , 106 ) ( 2018 , 111 )};
    \addlegendentry{Wind}

    %Solar
    \addplot coordinates { ( 2011 , 20 ) ( 2012 , 26 ) ( 2013 , 31 ) ( 2014 , 36 ) ( 2015 , 40 ) ( 2016 , 38 ) ( 2017 , 39 ) ( 2018 , 46 )};
    \addlegendentry{Solar}

    \end{axis}
    \end{tikzpicture}

    \caption{Energy from renewable sources in Germany}

    \label{fig:EnergyFromRenewablesGermany}
\end{figure}

标签: latexpdflatex

解决方案


nodes near coords style={font=\tiny}您可以使用(或您喜欢的任何尺寸)更改样式

\documentclass{book}

\usepackage{pgfplots}
\newcommand{\figureHeight}{0.5625} %% 16:9
\pgfplotsset{
    compat           = 1.13,
    grid             = major,
    enlarge x limits = 0,
%    cycle list name  = tum,
    major grid style = {dotted},
    minor grid style = {dotted},
    width            = \hsize,
    %width            = \hsize * 0.9,
    height           = \hsize * 0.9 * \figureHeight,
    legend style     = { at = {(0.98,0.96)}, anchor = north east,},
}

\begin{document}



\begin{figure}[thb]
    \centering

    \begin{tikzpicture}
    \begin{axis}[
    ybar               = 5pt, % configures `bar shift'
    xmajorgrids        = false,
    x tick label style = {
        /pgf/number format/1000 sep =},
    xtick              = { 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018},
    ylabel             = Energy Produced in TWh,
    enlarge x limits   = 0.1,
    ymin               = 0,
    ymax               = 125,
    bar width          = 4pt,
    legend style={at={(0.02,0.96)},anchor=north west},
    nodes near coords,
    nodes near coords style={font=\tiny}    
    ]

    %Hidro
    \addplot coordinates { ( 2011 , 18 ) ( 2012 , 21 ) ( 2013 , 23 ) ( 2014 , 19 ) ( 2015 , 19 ) ( 2016 , 21 ) ( 2017 , 20 ) ( 2018 , 19 )};
    \addlegendentry{Hydro}

    %Biomass
    \addplot coordinates { ( 2011 , 34 ) ( 2012 , 40 ) ( 2013 , 41 ) ( 2014 , 43 ) ( 2015 , 44 ) ( 2016 , 45 ) ( 2017 , 45 ) ( 2018 , 45 )};
    \addlegendentry{Biomass}

    %Wind
    \addplot coordinates { ( 2011 , 50 ) ( 2012 , 52 ) ( 2013 , 53 ) ( 2014 , 59 ) ( 2015 , 81 ) ( 2016 , 80 ) ( 2017 , 106 ) ( 2018 , 111 )};
    \addlegendentry{Wind}

    %Solar
    \addplot coordinates { ( 2011 , 20 ) ( 2012 , 26 ) ( 2013 , 31 ) ( 2014 , 36 ) ( 2015 , 40 ) ( 2016 , 38 ) ( 2017 , 39 ) ( 2018 , 46 )};
    \addlegendentry{Solar}

    \end{axis}
    \end{tikzpicture}

    \caption{Energy from renewable sources in Germany}

    \label{fig:EnergyFromRenewablesGermany}
\end{figure}



\end{document}

在此处输入图像描述


推荐阅读