Barchart

Tabellen und Grafiken erstellen und anordnen


Pusteblume
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Fr 18. Nov 2022, 10:33

Barchart

Beitrag von Pusteblume »

Hallo,

ich habe ein paar Probleme mit meinen Diagrammen. Hier mal das was ich bis jetzt habe:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{tikz}

\definecolor{hous}{HTML}{b88b4d}
\definecolor{green}{HTML}{79c561}
\definecolor{farming}{HTML}{ded94c}
\definecolor{trans}{HTML}{DF7401}
\definecolor{services}{HTML}{dbd4d3}
\definecolor{other}{HTML}{ff362e}
\definecolor{industry}{HTML}{db79c0}
\definecolor{water}{HTML}{7982db}
\definecolor{techinfra}{HTML}{303355}
\definecolor{zusatz1}{HTML}{4C0B5F}
\definecolor{zusatz2}{HTML}{0174DF}


\begin{document}

   \begin{figure} 
   \centering
   \begin{minipage}[t]{0.45\linewidth} 
    \centering 
    
     \begin{tikzpicture} 
        \begin{axis}[
        ybar=-0.6cm,
        height=5cm,
        %legend pos=outer north east,
         legend style={at={(0.5,-0.1)},anchor=north, 
         draw=none, legend cell align=left,
                    nodes={scale=0.7, transform shape}},
        axis x line*=bottom,
        axis y line*=left,
        bar width=0.4cm,
        ylabel={Number of responses},
        symbolic x coords={Test1,Test2,Test3, Test4, Test5,Test6},
        x tick label style={rotate=45, anchor=east, align=left},
        xtick=\empty,
        nodes near coords,
        nodes near coords align={vertical}          
        ]
        \addplot[hous,fill] coordinates {(Test1,4)};
        \addplot[green,fill] coordinates {(Test2, 3)};
        \addplot[farming,fill] coordinates {(Test3, 2)};
        \addplot[trans,fill] coordinates {(Test4, 1)};
        \addplot[services,fill] coordinates {(Test5, 0)};
        \addplot[other,fill] coordinates {(Test6, 1)};   
        \legend{Test1,Test2,Test3, Test4, Test5,Test6};
       
        \end{axis}
        
        \end{tikzpicture}   
        \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
    \label{findable}
    
   \end{minipage}
   \hfill 
   \begin{minipage}[t]{0.45\linewidth} 
    \centering
        
     \begin{tikzpicture} 
        \begin{axis}[
        ybar=-0.5cm,
        %legend pos=outer north east,
        legend columns=2,
        %legend pos=north east,
        legend style={at={(0.5,-0.1)},anchor=north, draw=none, legend cell align=left, % ?
                    %text depth=0pt,
                    nodes={scale=0.7, transform shape}},
        height=5cm,
        axis x line*=bottom,
        axis y line*=left,
        bar width=0.4cm,
        ylabel={Number of responses},
        symbolic x coords={Test1,Test2,Test3, Test4, Test5,Test6, Test7, Test8},
        x tick label style={rotate=45, anchor=east, align=left},
        xtick=\empty,
        nodes near coords,
        nodes near coords align={vertical}          
        ]
        \addplot[hous,fill] coordinates {(Test1,4)};
        \addplot[green,fill] coordinates {(Test2, 4)};
        \addplot[farming,fill] coordinates {(Test3, 3)};
        \addplot[trans,fill] coordinates {(Test4, 3)};
        \addplot[services,fill] coordinates {(Test5, 1)};
        \addplot[other,fill] coordinates {(Test6, 1)};   
        \addplot[zusatz1,fill] coordinates {(Test7, 1)};
        \addplot[zusatz2,fill] coordinates {(Test8, 1)}; 
        \legend{Test1,Test2,Test3, Test4, Test5,Test6, Test7, Test8}
                
        \end{axis}
        
        \end{tikzpicture}   
        \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
    \label{Interop}
   \end{minipage}
      
    \end{figure}

\end{document}

Dies ist wahrscheinlich nicht der eleganteste Weg die Abbildungen darzustellen und ich hab jetzt die beiden folgenden Probleme:
1. Ich hätte gerne dass die Balken im ersten Plot auch direkt an der X-Achse beginnen. Also das die 0 nicht noch nach oben verschoben wird.
2. Ich hätte gern dass die beiden Plots an der X-Achse ausgerichtet sind, also die X-Achsen jeweils auf der gleichen Höhe sind und es nicht nach den Legenden ausgerichtet ist (da die Einträge für die Legende bei dem ersten Plot sehr lang sind kann ich diese nicht in zwei Spalten packen).

Ich steh bei der Lösung dieser Probleme leider total auf dem Schlauch. Vielleicht kann ja jemand helfen.=)

Viele Grüße

gast.

Re: Barchart

Beitrag von gast. »

Für erstes, sei beispielsweise ymin=0 als Option empfohlen. Das zweite ist durch setzen einer passenden BoundingBox zu erreichen. Siehe dazu z.B. \useatboundingbox in der tikz-Anleitung.

Pusteblume
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Fr 18. Nov 2022, 10:33

Re: Barchart

Beitrag von Pusteblume »

Vielen Dank.
Ich wusste es gibt für das erste Problem eine einfache Lösung und ich hab sie nur übersehen.
\useatboundingbox werde ich mir mal genauer anschauen.
Danke für die Hilfe.=)

gast.

Re: Barchart

Beitrag von gast. »

Sorry: \useasboundingbox. Zuviel Stress!

Antworten