3

I want to remove the frame around a plot, which usually works well with axis lines = left. However, something strange happens when trying this with ybar.

Without axis lines=left

With axis lines=left

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread[col sep=tab]{data.tsv}\serialisations
\begin{axis}[
    ybar, xtick=data, xticklabels from table={\serialisations}{Name},
    axis lines=left, % remove for normal display
  ]
  \addplot table [x expr=\coordindex, y index=1]{\serialisations};
\end{axis}
\end{tikzpicture}

\end{document}

data.tsv

Name    Value
A   2000
B   3000
C   4000

How can I remove the frame, yet still have a correct graph?

4

ybar in conjuction with axis lines=left seems to set enlargelimits to false or 0.

You can use axis lines=left, enlargelimits=.1 or axis lines=left, enlargelimits=true to restore the default value.

enter image description here

Code:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=tab]{data.tsv}\serialisations
\begin{axis}[
    ybar, xtick=data, xticklabels from table={\serialisations}{Name},
    axis lines=left,
    enlargelimits=.1 % <- adds 10% of the axis range on both sides
  ]
  \addplot table [x expr=\coordindex, y index=1]{\serialisations};
\end{axis}
\end{tikzpicture}
\end{document}

It is also possible to change the x and the y limits separatly. You could use a dimension as abs value: enlarge x limits={abs={1.5*\pgfplotbarwidth}}. Additionaly you can enlarge only the upper limit of the y axis by enlarge y limits={0.1,upper} and define the lower y limit by ymin=1000.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=tab]{data.tsv}\serialisations
\begin{axis}[
    ybar, xtick=data, xticklabels from table={\serialisations}{Name},
    axis lines=left,
    enlarge x limits={abs={1.5*\pgfplotbarwidth}},
    enlarge y limits={0.1,upper},
    ymin=1000
  ]
  \addplot table [x expr=\coordindex, y index=1]{\serialisations};
\end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

|improve this answer|||||
  • Thanks, that indeed helps! Could you perhaps elaborate on why enlargelimits is necessary (i.e., why pgfplots doesn't do this automatically), and advise on how to choose the value? For larger plots, it seems that even smaller values of enlargelimits are needed. A possible drawback seems that my bar go floating above the x-axis. Maybe only enlarge x limits should be set? – Ruben Verborgh Mar 27 '15 at 9:59
  • I have changed my answer. But I do not know why ybar together with axis lines=left changes the enlargelimits option. – esdd Mar 27 '15 at 12:17
  • Great, very helpful answer! – Ruben Verborgh Mar 27 '15 at 12:47

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.