4

I have the following code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{testdata.dat}
  -3.316988309367815e-07 0.07000000000000001
  -1.948815911147526e-08 0.13
  7.15864175355987e-08 0
  1.799432483450127e-07 0
  0.0002850606647384129 0
  0.0009429559686380238 0.08999999999999997
  0.00157188462064142 0
  0.005259918700865051 0
  0.01739392641273605 0.18
  0.06208364670743995 0.55
  68.69485148165926 6.970000000000001
  31.217611224923 79.12
  0 12.89
\end{filecontents}

\pgfplotsset{compat=newest}
\pgfplotstableread{testdata.dat}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar interval,
  width=10cm,
  height=5cm,
  enlargelimits=false, 
  xtick=data,
  ymin=0, ymax=100,
  xticklabels={0 -- 0.063,0.063 -- 0.125,0.125 -- 0.25,0.25 -- 0.5,0.5 -- 1,
               1 -- 2,2 -- 4,4 -- 5.6,5.6 -- 8,
               8 -- 11.2,11.2 -- 16,16 -- 22.4,22.4 -- 31.5},
  x tick label style={rotate=60,anchor=east}
  ]
  \addplot table[x expr=\coordindex,y=0]{\datatable};
  \addplot table[x expr=\coordindex,y=1]{\datatable}; 
\end{axis}
\end{tikzpicture}
\end{document}

and the output looks like this:

enter image description here

But the command ybar interval does skip the last value. It should look like:

enter image description here

I prefer to have the last value to show up in the interval 22.4 -- 31.5. How do I achieve this without inserting a row of zeros to my table?

  • An interval needs a start and end value, so you need to have one more value than you need bars. Why are you using ybar interval, though? All your bars have the same width, so just using a normal ybar would work. – Jake Jul 5 '13 at 13:36
  • Yes I know, but I want to have the xticks inbetween, like in ybar interval. Is there a way to achieve this with ybar? – Reza Jul 5 '13 at 14:05
2

Because you want to combine different aspects of the plots, I guess a little manual labor can be accepted here. The main reason for not using ybar interval is mentioned by Jake in the comment above. The last data point is discarded as nothing follows it and hence no interval. But with ybar you want to achieve the visual equivalent of tick label positioning and that is possible.

However, ybar automatically shifts bars such that each bar group is centered around the tick. To override it, you can adjust the bar width and bar shifts manually. Then tick labels can be shifted via shift={(<coord>)}

Also you use xtick= data and enlargelimits=false and that makes the last data set disappear since it clips up to the final tick. Instead you can set the axis limits.

\documentclass[border=4mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}

\pgfplotstableread{
 -3.316988309367815e-07  0.07000000000000001
 -1.948815911147526e-08  0.13
  7.15864175355987e-08   0
  1.799432483450127e-07  0
  0.0002850606647384129  0
  0.0009429559686380238  0.08999999999999997
  0.00157188462064142    0
  0.005259918700865051   0
  0.01739392641273605    0.18
  0.06208364670743995    0.55
  68.69485148165926      6.970000000000001
  31.217611224923        79.12
  0                      12.89
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  width=10cm,
  height=5cm,
  ymin=0, ymax=100,
  xmin=0, xmax=13,
  xticklabels={0 -- 0.063,0.063 -- 0.125,0.125 -- 0.25,0.25 -- 0.5,0.5 -- 1,
               1 -- 2,2 -- 4,4 -- 5.6,5.6 -- 8,
               8 -- 11.2,11.2 -- 16,16 -- 22.4,22.4 -- 31.5},
  x tick label style={shift={(axis cs:0.5,0)},anchor=east,rotate=60,},
  bar width=0.5,
  xtick={0,...,12},enlargelimits=false
  ]
  \addplot+[bar shift=0.25] table[x expr=\coordindex,y index=0]{\datatable};
  \addplot+[bar shift=0.75] table[x expr=\coordindex,y index=1]{\datatable}; 
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Minor issues

  • pgfplotstable loads pgfplots which loads TikZ. So one is enough
  • Use the pgfplots version you have for compat key instead of newest
  • Consider logarithmic plots as a great portion of your data is practically invisible.

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.