TransWikia.com

How to set coordinates of curly braces with tikz

TeX - LaTeX Asked on August 3, 2021

It has been many years since the last time that I used latex but I am trying to engage again.

I put together a graph and I am trying to draw curly brackets vertically on the bars from y axes (for example 15 – until 25).

I am failing to get it right as I can not understand the coordinates.

I found this questions very useful but I am not able to understand how to do it Draw Curly Braces in TikZ

Sample of code:

documentclass{article}
usepackage{subcaption}

usepackage{pgfplots}
pgfplotsset{compat=newest}
usetikzlibrary{decorations.pathmorphing}

begin{document}

pgfplotstableread[row sep=,col sep=&]{
    interval  & traffic 
    Monday    &   20 
    Tuesday   &  100 
    Wednesday &   70 
    Thursday  &   40 
    Friday    &   80 
    Saturday  &   30 
    Sunday    &   30 
}mydata

begin{figure}[!htb]
    % begin{subfigure}{textwidth}
        begin{tikzpicture}
            begin{axis}[
                    ybar,
                    bar width=.5cm,
                    % width=textwidth,
                    height=.5textwidth,
                    legend style={at={(0.5,1)},
                                  anchor=north,
                                  legend columns=-1},
                    symbolic x coords={Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday},
                    xtick=data,
                    x tick label style={rotate=45,anchor=east},
                    nodes near coords,
                    nodes near coords align={vertical},
                    ymin=0,
                    ymax=140,
                    %xlabel={Car},
                    ylabel={Percentage %},
                ]
                addplot[yellow!10!black,fill=yellow!90!white] table [x=interval,y=traffic]{mydata};
                addplot[dashed,line legend,sharp plot,nodes near coords={},
                          update limits=false,shorten >=-3mm,shorten <=-3mm]
                          coordinates {(Monday,85) (Sunday,85)}
                          node[midway,above]{neutral};
                draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt]
                          (Monday) -- (Monday) node [black,midway,xshift=-0.6cm]
                          {footnotesize $P_1$}; % here is where it fails
                legend{Monitored Traffic}
            end{axis}
        end{tikzpicture}
        caption{Average Observed Traffic}
    % end{subfigure}
end{figure}
end{document}

Sample of output without the code that is failing:

enter image description here

Ps: is there a way to trim the lines on the top x axis?

One Answer

I propose a solution based on TikZ only. enter image description here

  • It uses the variable ys to scale the data along the y axis according to your needs.

  • The curly brace is introduced at the end of the drawing. Since I'm not using pgfplots, the coordinates are already the correct ones.

The code

documentclass[11pt, margin=1cm]{standalone}
usepackage{tikz}
usetikzlibrary[calc, math, decorations.pathreplacing]

begin{document}

tikzmath{
  integer Mo, Tu, We, Th, Fr, Sa, Su;
  real ys;
  Mo = 20;
  Tu = 100;
  We = 70;
  Th = 40;
  Fr = 80;
  Sa = 30;
  Su = 30;
  ys = .08;
}
begin{tikzpicture}[every node/.style={scale=.9}]
  % axes
  draw (0, 0) -- (8, 0);
  draw[->] (0, 0) -- (0, 110*ys)
  node[pos=.65, left=4em, rotate=90] {Percentage $%$};
  foreach j in {50, 100}{%
    draw (0, j*ys) -- ++(-3pt, 0) node[left] {$j$};
  }
  % legend
  path (8, 105*ys) node[draw, left]
  {tikz{draw[fill=yellow](0, 0) rectangle (.8ex, 1.8ex);} Monitored Traffic};
  
  % bars
  foreach d/name [count=j from 1] in {%
    Mo/Monday, Tu/Tusday, We/Wednesday, Th/Thursday, Fr/Friday,
    Sa/Saturday, Su/Sunday%
  }{%
    draw[fill=yellow]
    (j, -3pt) node[rotate=45, left] {name} -- ++(0, 3pt)
    ++(-.3, 0) rectangle ++(.6, d*ys) ++(-.3, 0) node[above] {$d$};
  }
  
  draw[thin, dashed] (-3pt, 85*ys) -- ++(7.5, 0)
  node[pos=.85, above] {neutral};

  % curly brace
  draw[red, decorate, decoration={brace, amplitude=1ex, raise=1ex}]
  (0, 15*ys) -- (0, 45*ys) node[pos=.5, left=2.5ex] {something};
end{tikzpicture}
end{document}

Correct answer by Daniel N on August 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP