Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I expand the x-axis by, say 1cm, in both directions in the following bar plot?

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,symbolic x coords={foo,bar,baz},x=2cm]
\addplot coordinates { (foo,1) (bar,3) (baz,2) };
\end{axis}
\end{tikzpicture}
\end{document}

The problem is that I cannot get enlarge x limits with abs value to work when using symbolic coordinates.

Using x=2cm to set the distance between the bars works fine, but how do I control the distance between the outer bars and the plot's boundary?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The key abs value=bar defines the value which will be used if enlarge limits is active. It will be ignored if it is not active.

Thus, activating enlarge limits will also activate the abs value. This can be done by means of enlarge x limits={true,abs value=bar}, or, equivalently, by means of enlarge x limits={abs=bar}.

In addition, you may want to use xtick=data - otherwise pgfplots tries to place tick marks between the bars and assigns them to one of the bars.

To summarize: enlarge x limits={abs=bar}, xtick=data does the job. It works because bar is the second symbolic coordinate. As such, it has index 1. Since enlargelimits necessarily needs numerical values, it uses the index of the symbolic coordinate. Thus, abs=bar causes the limits to be enlarged by one unit - which is 2cm in your case. Note that foo has index 0 and would not have worked.

This "numerics on symbols" certainly stretches the feature to its limits. If you feel that you want, say, a half unit, you should seriously consider to go back to numerical coordinates, combined with something like xtick={0,1,2}, xticklabel=foo,bar,baz. It has the same effect as symbolic x coords, although the data files are less "speaking".

share|improve this answer
    
This distinction between abs value,true and abs=true is certainly a bad example for a user interface, sorry about that. I admit, I did it. –  Christian Feuersänger Jul 23 '12 at 18:16
    
I tried to use the \pgfplotsconvertunittocoordinate{x}{1cm} but it's not responding. The idea that I had is that symbolic x coords still have an underlying numerical cs and we might be able to hack into it. –  percusse Jul 23 '12 at 18:32
    
Unfortunately, something like enlarge x limits={abs=bar} can only increase the limits by a mutiple of x. Is there a way to increase the limits by, say, 0.5*bar (i.e 1cm)? I find a little bit unfortunate that there seems to be no way to increase limits w.r.t. to a different coordinate system than axis cs. –  Andreas Jul 24 '12 at 7:38
1  
In my humble opinion, I believe that this "0.5*bar" is too much for the symbolic coordinates feature (what should that mean on its own!? Is it different from 0.5*foo ?). I understand what you want, though. If you want a different coordinate system in such cases, you may post a feature request on sourceforge. For the time being, I would suggest that you consider the solution mentioned in my last paragraph: to not use symbolic coordinates, and to place text labels manually. This allows numerical input. –  Christian Feuersänger Jul 24 '12 at 18:27

You can give enlarge x limits argument with a number between zero and one as a percentage. But also this would make the ticks repeat itself. To avoid that you also can give xtick=data option.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[enlarge x limits=0.3,xtick=data,ybar,symbolic x coords={foo,bar,baz},x=2cm]
\addplot coordinates { (foo,1) (bar,3) (baz,2) };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
OK, maybe I should have been clearer: relative distances (your enlarge x limits=0.3) won't work for me; I need absolute distances. Suppose I have a second bar plot with just the foo and bar bars; this plot's width would thus be smaller than the three-bar plot's. And while the distance between bars stays the same in both plots, the distance to the boundary doesn't, as it is computed relative to xmax - xmin. This is ugly. –  Andreas Jul 23 '12 at 15:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.