向条形图添加标签

向条形图添加标签

我试图在图表的条形图顶部添加标签,但不起作用。我希望它是显示的 y 值,但它却为条形图编号。

这是我目前的代码:

\documentclass[12pt]{article}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{eurosym}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{float}
\usepackage{caption}
\usepackage{calc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lastpage}
\usepackage{pdflscape}
\usepackage{times}
\captionsetup{justification=raggedright,singlelinecheck=false}
\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,
    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;  32; 0
        Curie;  4;  0
        Franklin;   6;  0
        Angela; 2;  0
        Ursula; 1;  0
        Warner; 5;  0
        Newton; 8;  0
        Hyems;  5;  0
        Repton; 1;  0
        };
        %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

和当前输出:条形图顶部的值与 y 坐标不匹配

答案1

我注释了代码以显示更改。只需删除该xbar行即可。我已经删除了此问题中无用的包。

在此处输入图片描述

\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;  32; 0
        Curie;  4;  0
        Franklin;   6;  0
        Angela; 2;  0
        Ursula; 1;  0
        Warner; 5;  0
        Newton; 8;  0
        Hyems;  5;  0
        Repton; 1;  0
        };
        %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

相关内容