# Enlarge limits by absolute value when using symbolic coordinates

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?

-

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".

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


-
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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8982177376747131, "perplexity": 1075.2459970059044}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2016-07/segments/1454701166141.55/warc/CC-MAIN-20160205193926-00260-ip-10-236-182-209.ec2.internal.warc.gz"}