Discontinuous x axis with pgfplots

Having a discontinuous y axis is common and Stackoverflow has a few solutions for that. I wanted an x axis with a gap (values 0-10 plus value 20). So this is what I did.

I create an axis from 0 to 12 and give 12 the label “20”. I add an extra tick on the x-axis at about halfway between 10 and “12”, where I want the gap and make it thick and white – basically I want a break in the axis. Then over that break I draw the “label” of this tick, which is two vertical lines at an angle, symbolizing the discontinuity. The relevant part of the style:

xmin=0,
xmax=12.5,
xticklabels={0, 2, 4, 6, 8, 10, 20},
extra x ticks={11.1},
extra x tick style={grid=none, tick style={white, very thick}, tick label style={xshift=0cm,yshift=.50cm, rotate=-20}},
extra x tick label={\color{black}{/\!\!/}},


And then I add the data with x-values 20 at x-coordinate “12”:

\addplot coordinates {
(0, 43.3) (1, 43.2) (2, 43.3) (3, 42.9) (4, 42.1) (5, 41.4)
(6, 41.2) (7, 41.7) (8, 41.7) (9, 42.1) (10, 42.1) };
\pgfplotsset{cycle list shift=-1}
\addplot coordinates { (12, 43.8) };
\draw[dotted] (axis cs:10, 42.1) -- (axis cs:12, 43.8);


Adding the last point separately from the rest of the data serves the purpose that I can draw the dotted line by hand. cycle list shift=-1 causes the new “plot” to have the same style as the previous. There might be a way of doing this, but this works.

Hat tip: Stackoverflow, but I currently cannot find the question(s) and answer(s) that helped me solve this. Still, thank you, anonymous people.

Encoding question mark in TikZ

I’m trying to draw the question mark that is sometimes displayed when there are encoding issues: �

This is my solution:

\tikz[baseline=(wi.base)]{
\node[fill=black,rotate=45,inner sep=.1ex,text height=1.8ex,text width=1.8ex] {};
\node[font=\color{white}] (wi) {?};
}


Marking significance in a bar plot

And still on the topic of LaTeX presentations, this time trying to plot a symbol over a bar to indicate significance.

This is how it works:

\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.47) {$\bullet$};


You need to put this code directly after the point where the data series has been plotted. Example:

\begin{tikzpicture}
\begin{axis}[xtick=data,axis x line*=bottom,axis y line=left,symbolic x coords={Xcoord1, Xcoord2}]

\addplot [ybar,seagreen] coordinates {(Xcoord1, -0.027) (Xcoord2, 0.436)};
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord2,0.47) {$\bullet$};

\addplot+ [ybar,blue] coordinates  {(Xcoord1, 0.331) (Xcoord2, 0.095)};
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.36) {$\bullet$};

\addplot+ [ybar,orange] coordinates {(Xcoord1, 0.222) (Xcoord2, 0.441)};
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.25) {$\bullet$};
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord2,0.47) {$\bullet$};
\end{axis}
\end{tikzpicture}


Overlays for bar charts (take 2)

A while back I posted about using overlays for bar charts to show one value at a time. For my latest presentation I had a similar but slightly different wish: show all values for one system at a time, one system after the other.

Easily done, I just adapt the code from my previous post to show all values at the same time:

\newcommand{\addplotoverlay}[3][]{
\alt<#3->{
}{
\addplot+ [ybar,#1] coordinates {(Xcoord1,0)}; % + don't show zero values in plot
}
}


This is specific to my plot, Xcoord1 is one of my symbolic x-coordinates in the plot. Other than that, the code is completely independent from the used coordinates and the number of them, which makes it more flexible than my old stuff.

Usage (this will let seagreen bars at the given coordinates appear on slide 2):

\addplotoverlayrank[seagreen]{(Xcoord1, 0.331) (Xcoord2, 0.095)}{2}


LaTeX ‘correct’ and ‘wrong’ symbols with TikZ

A symbol for a checkmark to indicate something is correct:

\newcommand{\correct}{$\color{green}\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;$}


A symbol for a cross to indicate something is wrong:

\newcommand{\wrong}{$\mathbin{\tikz [x=1.4ex,y=1.4ex,line width=.2ex, red] \draw (0,0) -- (1,1) (0,1) -- (1,0);}$}%


You’ll need TikZ for this.

Dependency trees with tikz-dependency

There is a package for drawing dependency trees in LaTeX called tikz-dependency:

\usepackage{tikz-dependency} % draw example with dependency tree


First the sentence is defined inside a deptext environment. You could add more rows to the sentence, e.g., for lemmas or parts-of-speech.
The edges between words are given with depedge commands (outside the deptext environment). Edges can in theory go both ways, but it looks better if they go from the head to the child. Here is an example dependency tree for a sentence:

\begin{dependency}
\begin{deptext}
It \& has \& a \& larger \& LCD \& than \& the \& T3i \& .\\
\end{deptext}
\deproot{3}{ROOT}
\depedge{2}{1}{NMOD}
\depedge{5}{3}{NMOD}
\depedge{5}{4}{NMOD}
\depedge{2}{5}{NMOD}
\depedge{5}{6}{PMOD}
\depedge{8}{7}{AMOD}
\depedge{6}{8}{PRD}
\end{dependency}


Besides drawing dependency trees, the package is also useful to create nice mark-up for words and phrases. The command is wordgroup which is inserted at the same place as the dependency edges and works upon the elements of the deptext The following draws a red box around words 7 and 8 in the sentence above (the 1 stands for the row):

\wordgroup[group style={fill=red!30, draw=red}]{1}{7}{8}{a}


There are lots of styling options for nodes, edges and word groups. I use the following in my thesis (this defines a style that combines options for both the dependency and the deptext environment, I just use the same in both):

\depstyle{depex}{%
edge style = {gray},
group style={inner sep=.2ex},
column sep=0.5em,
edge unit distance=2ex,
row sep=0.2em,
label style={draw=none,font=\scriptsize},
}


I want all mark-up of all word groups to be styled the same way, so I don’t want to write the part with fill and draw all the time with different colors. And if I want to change the percentage of white, I would like to do it at one place for all nodes. So I have written a macro that only expects the color to be given:

\tikzset{
coloring of/.style={fill=#1!30, draw=#1},
}


And finally, for presentations with dependency trees I want to be able to use overlays, i.e., to show a word group at a specific time. For edges you can use the normal \visible command from beamer, but for word groups it does not work for some reason. So this is a macro that does work and shows word groups only on specific slides (I got this from LaTeX Stack Exchange):

\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt={#1{}{invisible}}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}},
}


Finally, here an example of using the two macros:

\wordgroup[group style={coloring of=predicatecolor},visible on=<4->]{1}{2}{4}{pred}


Overlays for bar charts

Yesterday I posted about creating bar charts with TikZ and pgfplots.

Today I want to present a command to make the bars of one data series (i.e., one of my systems) appear one after the other on a beamer LaTeX slide.

This is the code to put into your preamble:

\newcounter{MyNextSlide}
\newcounter{MyNextNextSlide}
\setcounter{MyNextSlide}{#5}
\stepcounter{MyNextSlide}
\setcounter{MyNextNextSlide}{\theMyNextSlide}
\stepcounter{MyNextNextSlide}
\alt<#5->{\only<#5->{\alt<\theMyNextSlide->{\alt<\theMyNextNextSlide->{
\addplot+ [ybar,#1] coordinates {#2 #3 #4};
}{
}}{
}}}{
\addplot+ [ybar,#1] coordinates {(PI,0)}; % + don't show zero values in plot
}
}


Usage (‘first slide’ refers to the slide on which value 1 should first appear, it will stay and the slide afterwards will add value 2, the slide after that will add value 3):

\addplotoverlay [color or other options] {value 1}{value 2}{value 3}{first slide}


This depends on there being three data points in a data series and I have hardcoded the x coordinate PI. You’ll probably need to adjust this before you are able to do something useful with this code.

Bar charts in LaTeX with TikZ

I have four systems to compare (baseline, minimal, window, syntax) on three different tasks (let’s call them PI, AI and AC). I want a bar chart (similar to this example). We of course use TikZ and pgfplots and there is ybar to get a bar chart. The outer bars are cut off, so we need to add a little space on both sides with enlarge x limits. We can play around with the axes, the height and the width of the plot and the legend, but you can look at other examples for this, I’ll focus on two things here.

First, I would like to have the three tasks side by side with a nice name. In TikZ we can use symbolic x coordinates for this, we just give them some names and can then use them like any other x coordinate, e.g., to put a data point at (PI, 50). We can give the coordinates labels that are nicer to read with xticklabels. Usually there will be ‘ticks’ (i.e., markers on the x axis) somewhere randomly, to get only for each x-axis label/task, use xtick=data.

symbolic x coords={PI, AI, AC},
xticklabels={Pred. ident., Arg. ident., Arg. class.},
xtick=data,


Second, I would like to have the numbers above the bars with one decimal place. We can get the numbers with these two lines (the first one gives the numbers, as they are too big the second line adjusts the font size):

nodes near coords={\pgfmathprintnumber[fixed zerofill,fixed,precision=1]{\pgfplotspointmeta}}
every node near coord/.append style={font=\tiny}


To get rid of zeros, we can replace the second line with

every node near coord/.append style={
check for zero/.code={
\pgfmathfloatifflags{\pgfplotspointmeta}{0}{
\pgfkeys{/tikz/coordinate}
}{}
},
check for zero, font=\tiny},


So this is my final axis style:

\pgfplotsset{resultsplot/.style={
axis x line*=bottom,
axis y line=left,
ybar,
symbolic x coords={PI, AI, AC},
xticklabels={Pred. ident., Arg. ident., Arg. class.},
xtick=data,
enlarge x limits=0.2,
nodes near coords={\pgfmathprintnumber[fixed zerofill,fixed,precision=1]{\pgfplotspointmeta}},
every node near coord/.append style={
check for zero/.code={
\pgfmathfloatifflags{\pgfplotspointmeta}{0}{
\pgfkeys{/tikz/coordinate}
}{}
}, check for zero, font=\tiny},
area legend,
legend style={at={(0.5,-0.12)},
anchor=north,legend columns=-1},
}
}


And now we can get the actual graph that uses this axis style. Each plot represents a different system (the numbers are F1 scores):


\begin{tikzpicture}
\begin{axis}[resultsplot]
\addplot+ [ybar,green] coordinates {(PI, 67.8) (AI, 30.6) (AC, 20.2)};
\addplot+ [ybar,blue] coordinates {(PI, 78.6) (AI, 21.2) (AC, 16.5)};
\addplot+ [ybar,orange] coordinates {(PI, 80.0) (AI, 44.2) (AC, 36.6)};
\addplot+ [ybar,red] coordinates {(PI, 80.1) (AI, 54.2) (AC, 44.8)};
\end{axis}
\end{tikzpicture}


Have fun!

Align the top of a TikZ picture with the text

Usually the bottom of a tikzpicture is aligned with the baseline of the text. If you want to change that, you can use the option baseline on the picture. This lets the bottom of the tikzpicture be 5 ex below the baseline of the text:

\begin{tikzpicture}[baseline=-5ex]
\draw [red, very thick, dashed] (1,0) -- (0,0) -- (0,1);
\end{tikzpicture}


Instead of manually calculating the height of the drawing you want to include, you can use the provided current bounding box which is the rectangle containing all of the elements in the picture. This is a node with the usual anchors, i.e., north, south, east, west. To align the top of the box with the baseline of the text, use the following (note the parentheses, it’s a reference to a node!):

\begin{tikzpicture}[baseline=(current bounding box.north)]
...


Now probably you don’t want to align your picture with the baseline but with the top of the text. You can play around with the tikzpicture baseline further by introducing a yshift. For example a lowercase x is 1 ex high, so to align the top of the tikzpicture with the top of a lowercase x use a yshift of 1 ex (for uppercase letters or t, f, etc. try 1.6 ex). Be careful to copy all the different {( and [!:

\begin{tikzpicture}[baseline={([yshift={-1ex}]current bounding box.north)}]
...


Pie charts with LaTeX TikZ

Define a new command to insert a pie slice:

\newcommand{\slice}[4]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult

% slice
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

% outer label
\node[label=\midangle:#4] at (\midangle:1) {};

% inner label
\pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
\let\temp\pgfmathresult
\pgfmathparse{max(\temp,-0.5) + 0.8}
\let\innerpos\pgfmathresult
\node at (\midangle:\innerpos) {#3};
}


Then define the slices in the order you want to have them and with the percentages and labels. You can start at a different point in the circle by setting the counter ‘d’ to a different value before the loop, e.g. \setcounter{d}{25}.

\begin{tikzpicture}[scale=3]
\newcounter{c}
\newcounter{d}
\foreach \p/\t in {66/, 17/Equative, 10/Difference, 7/}
{
\setcounter{c}{\value{d}}