2

I created a pillar chart with TikZ. It has a percentage on the y-axis and an ordinary scale on the x-axis.

the diagram as it is now

I now calculated the average value by associating each ordinary value with an absolute value. How can I draw this average (as a vertical line) into the chart.

In the image, the average for the "original implementation" would be 1.875 which means I would like to draw a line between "low" and "low-medium".

Hear is my code:

\begin{tikzpicture}
    \begin{axis}[
    ybar, 
    ylabel=in \%, 
    bar width=20pt, 
    symbolic x coords={low, low-medium, medium-high, high, very-high},
    xtick={low, low-medium, medium-high, high, very-high}, 
    x tick label style={rotate=45, anchor=east, align=center},
    height=8cm,
    width=15cm,
    title={Coupling amount per class classified with low, low-medium, medium-high, high, very-high},
    nodes near coords,         
    nodes near coords align={vertical}, ]
        \addplot[draw = brown, fill = brown] coordinates{(low,87.5) (low-medium,12.5) (medium-high,0) (high,0) (very-high,0)};
        \addplot[draw = orange, fill = orange] coordinates{(low,100) (low-medium,0) (medium-high,0) (high,0) (very-high,0)};
        
        \legend{original implementation, FP implementation}
    \end{axis}
\end{tikzpicture}
1
  • Welcome to TeX.SX! Please, next time, use a complete MWE --- so that we have a compilable snippet with all the packages etc. needed to run (see the example in my answer).
    – Rmano
    Jul 29 at 11:06
2

I am not sure I understand: if the x coordinates are symbolic, saying that x=1.875 makes no sense: in symbolic coordinates, only the discrete values have a sense.

So you have two possibilities:

The first one is to use the rel axis cs: coordinate system inside the graphic. This is a coordinate system that starts from (0,0) at the bottom left and goes to (1,1) at the top right. You have to guess a bit the position, though... I added

\draw[thick, brown, dashed] (rel axis cs:{(1.875-0.5)/5},0) -- 
     (rel axis cs:{(1.875-0.5)/5},1);

enter image description here

The other one, if the values are really for example 1 for low, 2 for low-medium etc., you explicitly use a graph with the numerical x and then draw the line where you need with a draw:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
    ybar,
    ylabel=in \%,
    bar width=20pt,
    xmin=0.5, xmax=5.5,
    xtick={1,...,5},
    xticklabels ={low, low-medium, medium-high, high, very-high},
    x tick label style={rotate=45, anchor=east, align=center},
    height=8cm,
    width=15cm,
    title={Coupling amount per class classified with low, low-medium, medium-high, high, very-high},
    nodes near coords,
    nodes near coords align={vertical}, ]
        \addplot[draw = brown, fill = brown] coordinates{(1,87.5) (2,12.5) (3,0) (4,0) (5,0)};
        \addplot[draw = orange, fill = orange] coordinates{(1,100) (2,0) (3,0) (4,0) (5,0)};
        \draw[thick, brown, dashed] (1.875,0) -- (1.875,100);
        \legend{original implementation, FP implementation}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Thanks for your solution. The second approach you provided is the one I now use. I was not aware of the possibility to use the ordinal values while still using numerical values since it was my first time with tikzpicture.
    – Linde_98
    Jul 30 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.