在 beamer slides 中使用 pgfplot 绘制条形图

在 beamer slides 中使用 pgfplot 绘制条形图

我正在尝试在 Beamer 演示文稿中使用之前用于文章类型文档的条形图代码。我使用 TeXstudio 作为编辑器。每次我尝试编译时,应用程序都会冻结。似乎是 pgfplotsstableread 的问题,但我不确定是什么问题。我也需要在演示文稿上获取相同的图表。

以下是代码:

\usepackage{pgfplots, pgfplotstable}

\usetikzlibrary{backgrounds}
% background color definition from pgfmanual-en-macros.tex
\definecolor{graphicbackground}{cmyk}{0.04,0.02,0.02,0.04}
% key to change color
\pgfkeys{/tikz/.cd,
    background color/.initial=graphicbackground,
    background color/.get=\backcol,
    background color/.store in=\backcol,
}
\tikzset{background rectangle/.style={
        fill=\backcol,
    },
    use background/.style={    
        show background rectangle
    }
}


    \begin{center}

        % grid style
        %\pgfplotsset{grid style={solid, ultra thin,gray}}
        %\pgfplotsset{minor grid style={dashed,gray}}
        %\pgfplotsset{major grid style={dotted,green!50!black}}

        \begin{tikzpicture}[use background]



        \pgfplotstableread{ % Read the data into a table macro
            Label                                                      First   Second  
            {\small \textbf{HTML Design}}                                5     15
            {\small \textbf{UI \/ UX PSD files}}                         2     7
            {\small \textbf{Wireframes}}                                 0     3
        }\datatable

        \begin{axis}[
        xbar stacked,   % Stacked horizontal bars
        xmin=0,  xmax=15,       % Start x axis at 0
        title={\large \textbf {Gantt Chart }},
        height=9cm, width=12cm,
        bar width=0.8cm,
        axis x line*=bottom,
        axis y line*=left,
        y axis line style={opacity=1},
        enlarge y limits=true,
        xmajorgrids={true},
        grid style={
            solid,
            ultra thin,
            gray
        },
        tick style={tickwidth=0cm,major tick length=0cm},
        xlabel={\textbf{No of Sprints }},
        xtick ={5,10,15},
                %xminorgrids={true},
                %grid style={
                            %dashed,
                        %   ultra thin,
                        %   gray
            %   },
        %minor xtick ={1,3,5,7,9,11,13,15},
        %grid =both,
        ytick=data,     % Use as many tick labels as y coordinates
        yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
        ]
        \addplot [draw=none,fill=none] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
        \addplot [draw=none,fill=levelfirst]table [x=Second, y expr=\coordindex] {\datatable};


        \end{axis}


        \end{tikzpicture}


        \end{center}

答案1

使用像 这样的行结束标记row sep=\\。在数据表内包含格式详细信息并不是一个好主意。请在axis选项内使用它。

\pgfplotstableread[row sep=\\]{ % Read the data into a table macro
             Label                                      First   Second\\
            {HTML Design}                                5     15\\
            {UI \/ UX PSD files}                         2     7\\
            {Wireframes}                                 0     3\\
        }\datatable 

yticklabel style={font=\small\bfseries},

代码:

\documentclass{beamer}
\usepackage{pgfplotstable}

\usetikzlibrary{backgrounds}
% background color definition from pgfmanual-en-macros.tex
\definecolor{graphicbackground}{cmyk}{0.04,0.02,0.02,0.04}
% key to change color
\pgfkeys{/tikz/.cd,
    background color/.initial=graphicbackground,
    background color/.get=\backcol,
    background color/.store in=\backcol,
}
\tikzset{background rectangle/.style={
        fill=\backcol,
    },
    use background/.style={
        show background rectangle
    }
}

\begin{document}

\begin{frame}

        % grid style
        %\pgfplotsset{grid style={solid, ultra thin,gray}}
        %\pgfplotsset{minor grid style={dashed,gray}}
        %\pgfplotsset{major grid style={dotted,green!50!black}}
\begin{centering}
        \begin{tikzpicture}[use background]



        \pgfplotstableread[row sep=\\]{ % Read the data into a table macro
             Label                                      First   Second\\
            {HTML Design}                                5     15\\
            {UI \/ UX PSD files}                         2     7\\
            {Wireframes}                                 0     3\\
        }\datatable

        \begin{axis}[
        xbar stacked,   % Stacked horizontal bars
        xmin=0,  xmax=15,       % Start x axis at 0
        title={\large \textbf {Gantt Chart }},
        height=7cm, width=8cm,
        bar width=0.8cm,
        axis x line*=bottom,
        axis y line*=left,
        y axis line style={opacity=1},
        enlarge y limits=true,
        xmajorgrids={true},
        grid style={
            solid,
            ultra thin,
            gray
        },
        tick style={tickwidth=0cm,major tick length=0cm},
        xlabel={\textbf{No of Sprints }},
        xtick ={5,10,15},
                %xminorgrids={true},
                %grid style={
                            %dashed,
                        %   ultra thin,
                        %   gray
            %   },
        %minor xtick ={1,3,5,7,9,11,13,15},
        %grid =both,
        yticklabel style={font=\small\bfseries},
        ytick=data,     % Use as many tick labels as y coordinates
        yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
        ]
        \addplot [draw=none,fill=blue] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
        \addplot [draw=none,fill=red]table [x=Second, y expr=\coordindex] {\datatable};


        \end{axis}


        \end{tikzpicture}

\end{centering}
        \end{frame}
\end{document}

在此处输入图片描述

width其他小事情包括和填充颜色的调整height,我已经改变了。

类似地,对于titlexlabel,您可以更改格式,例如:

    title={Gantt Chart},
    title style={font=\large\bfseries},

    xlabel={No of Sprints},
    xlabel style={font=\bfseries},

相关内容