pgfplots:数字超出图表范围

pgfplots:数字超出图表范围

我对用来表示各个条形大小的数字有点问题。这些数字与实际图表相差甚远。不幸的是,直到现在我才发现自己的错误,并对其进行了相应的调整,因为图表变得更大,不再完全适合幻灯片。

\documentclass[10pt,%
aspectratio=169,%
]{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern}
\usepackage[english]{babel}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18,
    /pgf/number format/use comma,
    table/col sep = comma}

\usetikzlibrary{patterns} 

\begin{filecontents*}{tabellen/data1.csv} 
    Substanz,x,Messung1,AbwMessung1,Messung2,AbwMessung2,Messung3,AbwMessung3,Messung4,AbwMessung4
    Salzsäure [v\%/v],1,0.04,0.00,0.24,0.00,0.20,0.00,0.23,0.00
    Natronlauge [g/L],2,0.00,0.50,0.78,0.44,0.54,0.00,0.88,0.00
    Titanpentachlorid [g/L],3,0.00,0.00,1.39,0.05,0.00,0.00,0.99,0.00
    Isopropanol [g/L],4,0.00,0.00,0.05,0.00,0.06,0.00,0.04,0.00
    Benzol [g/mL],5,1.07,0.00,1.03,0.00,1.03,0.00,1.03,0.00
\end{filecontents*} 

\begin{document}
    \begin{frame}{Title}
        \begin{tikzpicture} 
            \begin{axis}[ 
                ybar=2pt,
                ylabel={Konzentration}, 
%               width=\textwidth, 
                height=\textheight,
                xticklabels from table={tabellen/data1.csv}{Substanz},       
                xtick=data, 
                ticklabel style={/pgf/number format/.cd, use comma, 1000 sep = {}}, 
                nodes near coords, 
                every node near coord/.append style={
                    yshift=transformdirectiony(\myshift+1),
                    anchor=center,
                    rotate=90,
                    font=\scriptsize
                },
                ymajorgrids=true, 
                legend pos= north east,
                legend cell align=right,
                ] 
                \addplot 
                [draw = blue, 
                fill = blue!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung1, 
                y error=AbwMessung1,
                visualization depends on=1.25*\thisrow{AbwMessung1} \as \myshift,
                ] 
                {tabellen/data1.csv}; 
                \addlegendentry{1. Messung}; 
                \addplot 
                [draw = red, 
                fill=red!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung2, 
                y error=AbwMessung2,
                visualization depends on=1.25*\thisrow{AbwMessung2} \as \myshift,
                ] 
                {tabellen/data1.csv}; 
                \addlegendentry{2. Messung}; 
                \addplot 
                [draw = green, 
                fill=green!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung3, 
                y error=AbwMessung3,
                visualization depends on=1.25*\thisrow{AbwMessung3} \as \myshift,
                ] 
                {tabellen/data1.csv};     
                \addlegendentry{3. Messung};
                \addplot 
                [draw = violet, 
                fill=violet!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung4, 
                y error=AbwMessung4,
                visualization depends on=1.25*\thisrow{AbwMessung4} \as \myshift,
                ] 
                {tabellen/data1.csv};     
                \addlegendentry{4. Messung};        
            \end{axis} 
        \end{tikzpicture} 
    \end{frame}
\end{document}

答案1

我建议不要使坐标的偏移取决于相应条的高度,而是使用固定的偏移,例如yshift=0.1cm

\documentclass[10pt,%
aspectratio=169,%
]{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[english]{babel}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18,
    /pgf/number format/use comma,
    table/col sep = comma}

\usetikzlibrary{patterns} 

\begin{filecontents*}[overwrite]{data1.csv} 
    Substanz,x,Messung1,AbwMessung1,Messung2,AbwMessung2,Messung3,AbwMessung3,Messung4,AbwMessung4
    Salzsäure [v\%/v],1,0.04,0.00,0.24,0.00,0.20,0.00,0.23,0.00
    Natronlauge [g/L],2,0.00,0.50,0.78,0.44,0.54,0.00,0.88,0.00
    Titanpentachlorid [g/L],3,0.00,0.00,1.39,0.05,0.00,0.00,0.99,0.00
    Isopropanol [g/L],4,0.00,0.00,0.05,0.00,0.06,0.00,0.04,0.00
    Benzol [g/mL],5,1.07,0.00,1.03,0.00,1.03,0.00,1.03,0.00
\end{filecontents*} 

\begin{document}
    \begin{frame}{Title}
        \begin{tikzpicture} 
            \begin{axis}[ 
                ybar=2pt,
                ylabel={Konzentration}, 
                width=\textwidth, 
                height=.8\textheight,
                xticklabels from table={data1.csv}{Substanz},       
                xtick=data, 
                ticklabel style={/pgf/number format/.cd, use comma, 1000 sep = {}}, 
                nodes near coords, 
                every node near coord/.append style={
%                    yshift=transformdirectiony(\myshift+1),
                    yshift=0.1cm,
                    anchor=west,
                    rotate=90,
                    font=\scriptsize
                },
                ymajorgrids=true, 
                legend pos= north east,
                legend cell align=right,
                ] 
               \addplot 
                [draw = blue, 
                fill = blue!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung1, 
                y error=AbwMessung1,
                visualization depends on=1.25*\thisrow{AbwMessung1} \as \myshift,
                ] 
                {data1.csv}; 
                \addlegendentry{1. Messung}; 
                \addplot 
                [draw = red, 
                fill=red!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung2, 
                y error=AbwMessung2,
                visualization depends on=1.25*\thisrow{AbwMessung2} \as \myshift,
                ] 
                {data1.csv}; 
                \addlegendentry{2. Messung}; 
                \addplot 
                [draw = green, 
                fill=green!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung3, 
                y error=AbwMessung3,
                visualization depends on=1.25*\thisrow{AbwMessung3} \as \myshift,
                ] 
                {data1.csv};     
                \addlegendentry{3. Messung};
                \addplot 
                [draw = violet, 
                fill=violet!30!white, 
                error bars/.cd, 
                y dir=both, 
                y explicit, 
                error mark=triangle*, 
                error bar style={color=black}]   
                table[ 
                x=x, 
                y=Messung4, 
                y error=AbwMessung4,
                visualization depends on=1.25*\thisrow{AbwMessung4} \as \myshift,
                ] 
                {data1.csv};     
                \addlegendentry{4. Messung};        
            \end{axis} 
        \end{tikzpicture} 
    \end{frame}
\end{document}

在此处输入图片描述

相关内容