如何用 tikz 设置花括号的坐标

如何用 tikz 设置花括号的坐标

距离我上次使用乳胶已经过去很多年了,但我正尝试再次使用。

我制作了一张图表,并尝试在 y 轴的条形上垂直绘制花括号(例如 15 - 直到 25)。

由于我无法理解坐标,所以无法正确理解。

我发现这些问题非常有用,但我不知道该怎么做在 TikZ 中绘制花括号

代码示例:

\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=.5\textwidth,
                    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}

不包含失败代码的输出示例:

在此处输入图片描述

附言:有没有办法修剪顶部 x 轴上的线条?

答案1

我仅基于此提出解决方案TikZ在此处输入图片描述

  • 它使用变量\ys来缩放数据轴可根据您的需要。

  • 绘图末尾引入了花括号。由于我没有使用pgfplots,所以坐标已经是正确的。

代码

\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}

相关内容