0

I'd like to put a second xlabel on a grouped bar graph using TiKz but can't seem to work it out. So I have found this cm superfonts package: http://ctan.org/pkg/cm-super. Or I can download cm fonts directly to my pc (but they're in a format I can't open and I've tried everything) http://sourceforge.net/projects/cm-unicode/

I'm intending to make my graph> export to Adobe illustrator or inkscape and write my group labels in there:

% This file was created by matlab2tikz v0.3.2.
% Copyright (c) 2008--2013, Nico Schlömer <nico.schloemer@gmail.com>
% All rights reserved.
%
% The latest updates can be retrieved from
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.
%
%
%

\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}

\begin{tikzpicture}

\begin{axis}[%
width=11.0276041666667in,
height=5.87479166666667in,
area legend,
scale only axis,
xmin=0, xmax=25,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40},
xticklabels={E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,},
ymin=0, ymax=2,
ylabel={CFU cm$^2$},
legend style={draw=black,fill=white,legend cell align=left},yticklabel style={/pgf/number format/fixed},
]
\addplot[ybar,bar width=0.10082380952381in,bar shift=-0.063014880952381in,fill=mycolor1,draw=black] plot coordinates{(1,0.04485625)
(2,1.881133333)
(3,1.197503086)
(4,1.842835802)
(5,0.3139072)
(6,0)
(7,0.04485625)
(8,0.002466667)
(9,0.578984568)
(10,0.003676543)
(11,0.3139072)
(12,0)
(13,0.000628571)
(14,0.001266667)
(15,0.576515432)
(16,0.001048457)
(17,0.30285028)
(18,0)
(19,0.04485625)
(20,0.001266667)
(21,0.576762346)
(22,0.002796914)
(23,0.30285028)};

\addlegendentry{4  ac.h$^{-1}$};

\addplot [
color=black,
solid,
forget plot
]
table{
0 0
25 0
};
\addplot[ybar,bar width=0.10082380952381in,bar shift=0.063014880952381in,fill=white,draw=black] plot coordinates{(1,0.04265625)
(2,0.595466667)
(3,0.499092593)
(4,0.401030556)
(5,0.170007298)
(6,0)
(7,0.04265625)
(8,0.000133333)
(9,0.306561728)
(10,0.000462346)
(11,0.170007298)
(12,0)
(13,5e-05)
(14,0.0002)
(15,0.306561728)
(16,0.000462346)
(17,0.159355735)
(18,0)
(19,0.04265625)
(20,0.0002)
(21,0.306592593)
(22,0.000777778)
(23,0.159355735)};

\addlegendentry{6 ac.h$^{-1}$};

\end{axis}
\end{tikzpicture}%
\end{document}

Like this: enter image description here

HCAI
  • 3,325
  • 1
    If you can export your data to a table file instead of using matlab2tikz, you could use the approach from http://tex.stackexchange.com/questions/55554/how-can-i-mix-an-ybar-and-an-ybar-stacked-with-pgfplots and its follow-up Draw group line error. – Jake Apr 18 '13 at 20:01
  • Your help as always is much appreciated! I have to say those two references are some seriously nice graphs! I have quite a few such graphs so I've eventually done this: \node[right, inner sep=0mm, text=black] at (axis cs:3, -0.2, 0) {Patient 1}; Any ideas (though this is an understatement :)) how to get a red line like above? – HCAI Apr 18 '13 at 20:24
  • 1
    I can't think of a good automatic way, but you can draw them manually by putting \pgfplotsset{ after end axis/.code={ \draw [red, thick] ({rel axis cs:0,0}-|{axis cs:6,0}) +(0pt,4ex) -- ++(0pt,-4ex); } } inside your axis environment. You could make that easier by wrapping the code in a new command. – Jake Apr 18 '13 at 20:30
  • Inside the axis environment? When I compile using pdflatex it spits out an error saying capacity exceeded. even if I put in \usepackage{etex} \reserveinserts{28}. But that aside I've just realised the graph itself is 11"x5". Altering the size in the axis causes all sorts of nastiness. How would you scale it so that the spacing between bars remains constant? – HCAI Apr 18 '13 at 20:48

1 Answers1

2

Well, better late than never! :-)

This solution relies on modifying the code that's generated by matlab2tikz, so it may or may not be acceptable for you.

I'll show the whole code and output first, and then explain my changes.

\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}

\begin{tikzpicture}

\begin{axis}[%
width=11.0276041666667in,
height=5.87479166666667in,
area legend,
scale only axis,
clip=false, % do not clip items drawn outside the axis (Patient n labels)
xmin=0, xmax=25,
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40},
xticklabels={E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,,E,P,H,N,F,},
ymin=0, ymax=2,
ylabel={CFU cm$^2$},
legend style={draw=black,fill=white,legend cell align=left},yticklabel style={/pgf/number format/fixed},
extra x ticks={6,12,...,24}, % locations of the red lines
extra x tick style={ % style for the extra red lines
  tickwidth=6ex, % length of the extra red lines from top to bottom
  tick align=center, % draw extra red lines centered about the axis grid
  tick style={ultra thick,red}, % set line thickness and color
  xticklabel={}, % do not label the extra ticks
  xtick pos=left, % draw extra ticks only on the "left" (bottom), not the top
},
]
\addplot[ybar,bar width=0.10082380952381in,bar shift=-0.063014880952381in,fill=mycolor1,draw=black] plot coordinates{(1,0.04485625)
(2,1.881133333)
(3,1.197503086)
(4,1.842835802)
(5,0.3139072)
(6,0)
(7,0.04485625)
(8,0.002466667)
(9,0.578984568)
(10,0.003676543)
(11,0.3139072)
(12,0)
(13,0.000628571)
(14,0.001266667)
(15,0.576515432)
(16,0.001048457)
(17,0.30285028)
(18,0)
(19,0.04485625)
(20,0.001266667)
(21,0.576762346)
(22,0.002796914)
(23,0.30285028)};

\addlegendentry{4  ac.h$^{-1}$};

\addplot [
color=black,
solid,
forget plot
]
table{
0 0
25 0
};
\addplot[ybar,bar width=0.10082380952381in,bar shift=0.063014880952381in,fill=white,draw=black] plot coordinates{(1,0.04265625)
(2,0.595466667)
(3,0.499092593)
(4,0.401030556)
(5,0.170007298)
(6,0)
(7,0.04265625)
(8,0.000133333)
(9,0.306561728)
(10,0.000462346)
(11,0.170007298)
(12,0)
(13,5e-05)
(14,0.0002)
(15,0.306561728)
(16,0.000462346)
(17,0.159355735)
(18,0)
(19,0.04265625)
(20,0.0002)
(21,0.306592593)
(22,0.000777778)
(23,0.159355735)};

\addlegendentry{6 ac.h$^{-1}$};
\foreach \locn/\num in {3/1,9/2,15/3,21/4} {
  \edef\temp{\noexpand\path (xticklabel cs:0) -| (\locn,0) node[pos=0.5] {Patient \num};}
  \temp % solution from https://tex.stackexchange.com/questions/24539/
}
\end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

Vertical Red Lines

I decided to draw the red lines by making them extra x ticks and applying an appropriate style. The relevant code is towards the end of the axis environment options list, reproduced here:

extra x ticks={6,12,...,24}, % locations of the red lines
extra x tick style={ % style for the extra red lines
  tickwidth=6ex, % length of the extra red lines from top to bottom
  tick align=center, % draw extra red lines centered about the axis grid
  tick style={ultra thick,red}, % set line thickness and color
  xticklabel={}, % do not label the extra ticks
  xtick pos=left, % draw extra ticks only on the "left" (bottom), not the top
},

The comments describe the function of each setting. You can customize these to your liking, of course.

Patient Labels Below

For the group labels (Patient 1, 2, etc.), I added clip=false to the axis options list to prevent these extra labels from being clipped away.

The remainder of the code necessary to place these labels is in the form of a \foreach loop at the very end of the axis environment:

\foreach \locn/\num in {3/1,9/2,15/3,21/4} {
  \edef\temp{\noexpand\path (xticklabel cs:0) -| (\locn,0) node[pos=0.5] {Patient \num};}
  \temp % solution from https://tex.stackexchange.com/questions/24539/
}

I used a solution from How can I use \foreach in an axis environment of pgfplots? to work around some expansion difficulties with \foreach inside axis environments. The expansion of \temp will mark a -| \path from the bottom of the tick labels to the desired x-coordinate of the "Patient n" label. The node is then placed below the mid-point of this path (the turning point).

Paul Gessler
  • 29,607