# pgfplots bar chart enlarge axis [duplicate]

I would like to create a bar chart using tikz. I want it to look like that:

but when I added the data, I got something like that:

Is there a way to enlarge the x-axis over the textwidth or some other way to fix it? I would like the text to stay at this size.

Here is my code:

\begin{tikzpicture}
\begin{axis}[
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar,
bar width=10pt,
%
point meta=rawy,
%
axis x line=bottom,
axis y line=left,
ymajorgrids=true,
grid style=dashed,
%
ylabel=[MWh],
ymin=0,
scaled ticks=false,
tick label style={/pgf/number format/fixed},
enlargelimits=auto,
enlarge x limits=0.2,
%
symbolic x coords ={2021, 2022, 2023, 2024, 2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,2035,2036},
%x tick label style={rotate=30,anchor=north east},
xtick distance=1,
/pgf/number format/.cd,
use comma,
1000 sep={.}
]
(2021,232)
(2022,224)
(2023,216)
(2024,208 )
(2025, 200)
(2026,192)
(2027,184)
(2028,176)
(2029,168)
(2030,160)
(2031, 152)
(2032,144)
(2033,136)
(2034, 128)
(2035,120)
(2036,112)
};
(2021,198)
(2022,193)
(2023,188)
(2024,182)
(2025,177)
(2026,172)
(2027,167)
(2028,162)
(2029,157)
(2030,152)
(2031,147)
(2032,142)
(2033,137)
(2034, 132)
(2035,127)
(2036, 122 )
};
(2021,122)
(2022,134)
(2023,147)
(2024,160)
(2025,172)
(2026,185)
(2027,198)
(2028,210)
(2029,223)
(2030, 236)
(2031, 248)
(2032,261)
(2033,274)
(2034, 287)
(2035,299)
(2036,312)
};
(2021,300)
(2022,317)
(2023,335)
(2024,353)
(2025,370)
(2026,388)
(2027,405)
(2028, 423)
(2029, 441)
(2030,458)
(2031, 476)
(2032, 493)
(2033,511)
(2034,528)
(2035, 546)
(2036,564)
};
\legend{Option 1, Option 2, Option 3, Option 4}
\end{axis}
\end{tikzpicture}


I'm sorry, if the question has been asked before, I just couldn't find anything that fit my answer. Thanks in advance!

• width=0.9\textwidth and/or height=<length> in the axis options. If you only specify one the aspect ratio remains the same. Quite possibly a duplicate can be found, if not me (or whoever else feels like doing so) can add an answer later. Nov 25 at 17:32

I think I've answered something along the same lines before, but couldn't find it now, so here goes.

As mentioned in my comment, you use the width and height keys in the axis options to set the approximate1 size of the diagram.

Obviously, whether all your bars actually fit inside the available space, depends on a few things:

• how wide the axis is (set by width=<length>)
• how many bars you have
• the width of the bars (set by bar width=<length>)
• the space between the bars (set by the argument to ybar, e.g. ybar=1pt)

The exact choices for these will depend on how your document is set up, how wide the text block is. The choices made in the example below is for a standard article with A4 paper.

You say you want the text to remain the same size, and presumably that includes the xticklabels. With a standard article there is not enough room to have all the ticklabels horizontally. If your document has a wider text block there may be room. I guess the options are to either not print all the labels (shown below), or rotate the labels. If you prefer that option, uncomment the lines with xtick distance=1 and x tick label style={..}. You'll also need to move the legend down a bit.

Note that when you have a ] in a value in the option list, as in ylabel=[MWh], you need to wrap the value in braces, otherwise pgfplots sees the ] as the closing bracket for the axis options, so everything after it is basically ignored. I.e., do ylabel={[MWh]}.

I also removed the symbolic coords, I don't really see the need for them here, when you have evenly spaced numerical x-values.

1: pgfplots doesn't calculate an exact size of the entire diagram, including all labels. It simply allocates a fixed amount of space, 45pt, for the labels. Hence, if you set width=345pt, what pgfplots actually does is to set the size of the axis box alone (not including labels) to 300pt, and the total width is 300pt + whatever amount of space the labels occupy. If you set scale only axis, then the width/height apply to the axis box alone.

\documentclass[a4paper,svgnames]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % or whatever version you have
\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=\textwidth,    % set width
height=0.55\textwidth, % and height
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar=0.5pt, % reduce space between bars
bar width=3pt,   % they were too wide
%
point meta=rawy,
%
axis x line=bottom,
axis y line=left,
ymajorgrids=true,
grid style=dashed,
%
ylabel={[MWh]}, % wrap in {} when you have []
ymin=0,
scaled ticks=false,
enlarge x limits={abs=0.6},
%% if you want xtick distance=1 you either need a wider textblock, or rotate the ticklabels as you've done before
%x tick label style={rotate=30,anchor=north east},
% xtick distance=1,
/pgf/number format/.cd,
use comma,
1000 sep={.}
]
(2021,232)
(2022,224)
(2023,216)
(2024,208 )
(2025, 200)
(2026,192)
(2027,184)
(2028,176)
(2029,168)
(2030,160)
(2031, 152)
(2032,144)
(2033,136)
(2034, 128)
(2035,120)
(2036,112)
};
(2021,198)
(2022,193)
(2023,188)
(2024,182)
(2025,177)
(2026,172)
(2027,167)
(2028,162)
(2029,157)
(2030,152)
(2031,147)
(2032,142)
(2033,137)
(2034, 132)
(2035,127)
(2036, 122 )
};
(2021,122)
(2022,134)
(2023,147)
(2024,160)
(2025,172)
(2026,185)
(2027,198)
(2028,210)
(2029,223)
(2030, 236)
(2031, 248)
(2032,261)
(2033,274)
(2034, 287)
(2035,299)
(2036,312)
};
(2021,300)
(2022,317)
(2023,335)
(2024,353)
(2025,370)
(2026,388)
(2027,405)
(2028, 423)
(2029, 441)
(2030,458)
(2031, 476)
(2032, 493)
(2033,511)
(2034,528)
(2035, 546)
(2036,564)
};
\legend{Option 1, Option 2, Option 3, Option 4}
\end{axis}
\end{tikzpicture}

\end{document}

• You are right. This is a quite common question here. I will close the question as duplicate where I state some of my answers to similar questions. Of course feel free to extend the list when you find yours ;) Nov 26 at 6:30
• thanks a lot, that works perfect! :) yesterday