使所有条形标签显示在条形上方,而不使用科学计数法

使所有条形标签显示在条形上方,而不使用科学计数法

目前,我的条形图中的所有标签都没有显示在条形上方,我不知道为什么。此外,标签似乎自动使用科学记数法,我也不知道如何更改。

这是我当前的代码:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}

\tikzstyle{chart}=[
legend label/.style={font={\Large},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=10pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}}]

\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
\begin{minipage}{0.96\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar,%ybar legend,
fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
%    xbar, <--- comment this line 
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    symbolic x coords={Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton},
    xtick=data,
    nodes near coords,
    yticklabel style={/pgf/number format/fixed},,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    xtick distance=0.2,
    ytick distance=,
    x tick label style ={font=\normalsize,text width=0.7cm,anchor=north,rotate=0,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    scaled y ticks=false,
    bar width=45pt,
    ymajorgrids,
    xlabel=\textbf{Teams},
    ylabel=\textbf{Points},
    title=\textbf{Team Points},
    ,
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z
        Total;  17000000; 0
        Curie;  4000000;  0
        Franklin;   6000000;  0
        Angela; -3000000;  0
        Ursula; 1000000;  0
        Warner; 5000000;  0
        Newton; 8000000;  0
        Hyems;  -5000000;  0
        Repton; 1000000;  0
        };
        %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

和当前输出:

在此处输入图片描述

我希望所有标签都位于条形图上方,并且不采用科学记数法。

答案1

这对于数字格式很有效:\pgfkeys{/pgf/number format/fixed}

将其更改为\pgfkeys{/pgf/number format/.cd,fixed,precision=0}另外可解决以下评论中的舍入请求。

我在下面的工作示例中标记了该行

对于条形图上方的标签,我通过 引入了一个假列\pgfplotstableset{creat on use ...},这需要事先将表格读入宏中。实际上pgfplotstableread可能不需要这样做。最后,第二个图覆盖实际的条形图,从而生成标签。因此,该nodes near coords选项从轴选项移到了特定图。否则,第一个图也会在图表下方创建标签。

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}

\tikzstyle{chart}=[
legend label/.style={font={\Large},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=10pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}}]

\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
    \begin{minipage}{0.96\linewidth}
        \flushleft
        \begin{tikzpicture}
            \pgfplotscreateplotcyclelist{defaultCycle}{%
                ybar,%ybar legend,
                fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
            }
            \pgfkeys{/pgf/number format/.cd,fixed,precision = 0}  % <--- add this line
            
            \pgfplotstableread[col sep=semicolon] {
                x;  y;  z
                Total;  17000000.4; 0
                Curie;  4000000.5;  0
                Franklin;   6000000;  0
                Angela; -3000000;  0
                Ursula; 1000000;  0
                Warner; 5000000;  0
                Newton; 8000000;  0
                Hyems;  -5000000;  0
                Repton; 1000000;  0
            } \data
            
            \pgfplotstableset{
                create on use/labelY/.style={
                    create col/expr={\thisrow{y}>0)*\thisrow{y}}
                }
            }
            
            \begin{axis}
                [
                %    xbar, <--- comment this line 
                cycle list name=defaultCycle,
                width=0.96\linewidth,
                height=0.15\textheight,
                use units,
                scale only axis,
                symbolic x coords={Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton},
                xtick=data,
                yticklabel style={/pgf/number format/fixed},,
                ytick pos=left,
                axis y line*=left,
                xtick pos=bottom,
                axis x line*=bottom,
                legend style={draw=none,at={(0,1.03)},anchor=south west},
                legend columns=-1,
                xtick align=center,
                ytick align=center,
                xtick distance=0.2,
                ytick distance=,
                x tick label style ={font=\normalsize,text width=0.7cm,anchor=north,rotate=0,align=center},
                y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
                scaled y ticks=false,
                bar width=45pt,
                ymajorgrids,
                xlabel=\textbf{Teams},
                ylabel=\textbf{Points},
                title=\textbf{Team Points},
                ,
                ]
                \addplot+ table [x={x},y={y}] {\data};
                \addplot+[
                    nodes near coords,
                    nodes near coords align = above,
                    point meta=explicit
                    ] table [x={x},y={labelY}, meta = {y} ] {\data};
                %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
            \end{axis}
        \end{tikzpicture}
    \end{minipage}
    
\end{document}

结果

相关内容