TransWikia.com

PGFPlots - How to make multiline axis labels with non-boxed axis

TeX - LaTeX Asked on October 3, 2021

This answer describes how to create multiline axis labels for boxed axes. However, when I try to do this with non-boxed axes it fails. For example,

documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.16}
begin{document}
begin{tikzpicture}
  begin{axis}[
  title=MWE,
  xlabel style={align=center}, xlabel={somexlabel},
  ylabel style={align=center}, ylabel={someylabel},
  axis x line=middle,
  % axis y line=middle,
  xtick=empty,
  ytick=empty,
]
  addplot[
    samples=100,
    domain=-2:2,
  ] {x};
end{axis}
end{tikzpicture}
end{document}

Produces
enter image description here

How can I get the x-axis label to also span 3 lines?

I’m aware that the question linked above also discusses the use of text width as a possible solution. However, replacing xlabel style=... in my example with xlabel style={align=center, text width=2cm} does not change the result.

2 Answers

The "problem" with your code is that you change axis x line after you have changed the xlabel style and thus your change is overwritten. So just change the axis x line first and you'll receive the result you expect.

% used PGFPlots v1.17
documentclass[border=5pt]{standalone}
usepackage{pgfplots}
begin{document}
begin{tikzpicture}
  begin{axis}[
    title=MWE,
    axis x line=middle,     % <-- moved up
    xlabel style={align=center}, xlabel={somexlabel},
    ylabel style={align=center}, ylabel={someylabel},
    xtick=empty,
    ytick=empty,
]
  addplot[
    samples=100,
    domain=-2:2,
  ] {x};
end{axis}
end{tikzpicture}
end{document}

image showing the result of above code

Correct answer by Stefan Pinnow on October 3, 2021

xlabel is printed as node. So you can set a specific text width. This allows line breaks. The modification can be done by the option xlabel style. An example is shown in the documentation on page 173:

xlabel style={align=right,text width=3cm}

enter image description here

documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.16}
begin{document}
    begin{tikzpicture}
    begin{axis}[
    title=MWE,
    xlabel style= xlabel style={align=left,text width=2cm},{align=center}, xlabel={somexlabel},
    ylabel style={align=center}, ylabel={someylabel},
    axis x line=middle,
    % axis y line=middle,
    xtick=empty,
    ytick=empty,
    ]
    addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    end{axis}
    end{tikzpicture}
end{document}

EDIT

As long as the xaxis and yaxis are at the default pos that is bottom and left respectively the multiline is obeyed for the axis labels but if the yaxis or xaxis is moved to the center/middle the multiline environ does not behave and we get a single line axis label

In the example below moving the yaxis to middle but leaving the xaxis at default bottom pos-- here the xaxis retains multiline environ but not so for the yaxis

enter image description here

documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.16}
begin{document}
    begin{tikzpicture}
    begin{axis}[xlabel style={
                                align=left,
                                text width=3cm
                                },
                xlabel=A quite long label with a line break,
                title=MWE,
%               xlabel style={
%                               align=center,
%                               text width=1cm
%                               },
%               {align=center}, 
%               xlabel={some x label},
                ylabel style={align=center}, 
                ylabel={someylabel},
%               axis x line=middle,
                 axis y line=middle,
%               xtick=empty,
%               ytick=empty,
                ]
    addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    end{axis}
    end{tikzpicture}
end{document}

Now if xaxis also moves to the middle the multiline here also is not obeyed

enter image description here

documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.16}
begin{document}
    begin{tikzpicture}
    begin{axis}[xlabel style={
                                align=left,
                                text width=3cm
                                },
                xlabel=A quite long label with a line break,
                title=MWE,
%               xlabel style={
%                               align=center,
%                               text width=1cm
%                               },
%               {align=center}, 
%               xlabel={some x label},
                ylabel style={align=center}, 
                ylabel={someylabel},
                axis x line=middle,
                 axis y line=middle,
%               xtick=empty,
%               ytick=empty,
                ]
    addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    end{axis}
    end{tikzpicture}
end{document}

Probably one of the legends will clarify

Answered by js bibra on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP