蓝图柱状图与 TikZ 适配问题

蓝图柱状图与 TikZ 适配问题

我使用以下工作代码作为我的愿望的蓝图。

\begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=20pt,
        %
        nodes near coords,
        nodes near coords align=above,
        point meta=rawy,
        %
        axis x line=bottom,
        axis y line=left,
        ymajorgrids=true,
        %
        ylabel=$\mathrm{kWh/m^2a}$,
        ymin=0,
        ytick={0,50,100,150,200,250,300},
        enlargelimits=auto,
        %
        xlabel= Sanierter Geb\"audeteil,
        symbolic x coords ={Unrenoviert,Fenster,H\"ulle,Bodenplatte,Heizung},
        x tick label style={rotate=30,anchor=north east},
        ]
    
        \addplot[fill=green] coordinates {
          (Unrenoviert,220.6)
          (Fenster,219.26)
          (H\"ulle,197.67)
          (Bodenplatte,167.9)
          (Heizung,40)
        };
      \end{axis} 
    \end{tikzpicture}

我将该行改为symbolic x coords ={Unrenoviert,Fenster,H\"ulle,Bodenplatte,Heizung}symbolic x coords ={Unter 18, 18-24, 25-34, 35-44, 45-54, 55 bis 64, 65 und älter}因为我需要其他 symbolix 功能并出现错误。

还有一些其他错误。我想将 Excel Grafik 转换为 LaTex。对我来说,这似乎比想象的要难。如果您能给我一些提示,那就太好了。谢谢!

 \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=20pt,
        %
        nodes near coords,
        nodes near coords align=above,
        point meta=rawy,
        %
        axis x line=bottom,
        axis y line=left,
        ymajorgrids=true,
        %
        ylabel=Anzahl,
        ymin=0,
        ytick={0,1,2,3,4},
        enlargelimits=auto,
        %
        xlabel= Alter,
        symbolic x coords ={{Unter 18}, {18-24}, {25-34}, {35-44}, {45-54}, {55 bis 64}, {65 und \"alter}},
        x tick label style={rotate=30,anchor=north east},
        ]
        \addplot[fill=green] coordinates {
          ({Unter 18}, 0)
          ({18-24}, 1)
          ({25-34}, 4)
          ({35-44}, 0)
          ({45-54}, 2)
          ({55-64}, 0)
          ({65 und \"alter}, 0)
        };
      \end{axis} 
    \end{tikzpicture}

我收到此错误:

! Package pgfplots Error: Sorry, the input coordinate `55-64' has not been defi
ned with 'symbolic x coords={{Unter 18}, {18-24}, {25-34}, {35-44}, {45-54}, {5
5 bis 64}, {65 und \"alter}}... Maybe it has been misspelled? Or did you mean s
omething like [normalized]55-64?.
    

在此处输入图片描述

答案1

您需要用括号将包含特殊字符(例如空格等)[的文本括起来。]{...}

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
  \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=20pt,
        %
        nodes near coords,
        nodes near coords align=above,
        point meta=rawy,
        %
        axis x line=bottom,
        axis y line=left,
        ymajorgrids=true,
        %
        ylabel=$\mathrm{kWh/m^2a}$,
        ymin=0,
        ytick={0,50,100,150,200,250,300},
        enlargelimits=auto,
        %
        xlabel= Sanierter Geb\"audeteil,
        symbolic x coords ={{Unter 18}, {18-24}, {25-34}, {35-44}, {45-54}, {55 bis 64}, {65 und älter}},
        x tick label style={rotate=30,anchor=north east},
        ]
        \addplot[fill=green] coordinates {
          (Unrenoviert,220.6)
          (Fenster,219.26)
          (H\"ulle,197.67)
          (Bodenplatte,167.9)
          (Heizung,40)
        };
      \end{axis} 
    \end{tikzpicture}
\end{document}

在此处输入图片描述

编辑

我认为您可以分成symbolic x coordsxtickxticklabels使用真实坐标。

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
 \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=20pt,
        %
        nodes near coords,
        nodes near coords align=above,
        point meta=rawy,
        %
        axis x line=bottom,
        axis y line=left,
        ymajorgrids=true,
        %
        ylabel=Anzahl,
        ymin=0,
        ytick={0,1,2,3,4},
        enlargelimits=auto,
        %
        xlabel= Alter,
%         symbolic x coords ={{Unter 18},{18-24},{25-34},{35-44},{45-54},{55 bis 64},{65 und \"alter}},
        xtick={1,2,...,7},
        xticklabels={{Unter 18},{18-24},{25-34},{35-44},{45-54},{55 bis 64},{65 und \"alter}},
        x tick label style={rotate=30,anchor=north east},
        ]
        \addplot[fill=green] coordinates {
          (1, 0)
          (2, 1)
          (3, 4)
          (4, 0)
          (5, 2)
          (6, 0)
          (7, 0)
        };
      \end{axis} 
    \end{tikzpicture}
\end{document}

在此处输入图片描述

编辑2

删除这些行即可删除栏上的文本。

nodes near coords,
nodes near coords align=above,
point meta=rawy,

添加title=Title以添加标题。

检查您是否有这样的代码\pgfplotsset{compat=1.17},如果没有,请添加到您的代码中,然后Alter就会在正确的位置。

最后建议大家学会在文档中搜索关键词,这样会更快的得到帮助。 在此处输入图片描述

相关内容