pgfplots 条形图放大轴

pgfplots 条形图放大轴

我想使用 tikz 创建条形图。我希望​​它看起来像这样:

在此处输入图片描述

但是当我添加数据时,我得到了如下结果: 在此处输入图片描述

有没有办法将 x 轴放大到超过文本宽度,或者有其他方法可以修复它?我希望文本保持这个大小。

这是我的代码:

\begin{tikzpicture}
\begin{axis}[ 
compat=newest,   
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={.}
]
\addplot[fill=Grey] coordinates {   
(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)
};   
\addplot[fill=LightBlue] coordinates {      
(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 )      
};   
\addplot[fill=Blue] coordinates {    
(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)      
};   
\addplot[fill=DarkBlue] coordinates {          
(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} 

抱歉,如果这个问题之前有人问过,我找不到任何适合我的答案。提前谢谢!

答案1

我想我之前回答过类似的事情,但现在找不到了,所以就在这里。

正如我在评论中提到的,您可以使用选项中的width和键来设置图表的近似1尺寸。heightaxis

显然,所有酒吧是否真的适合可用空间,取决于以下几个因素:

  • 轴的宽度(由 设置width=<length>
  • 你有多少个酒吧
  • 条形的宽度(由 设置bar width=<length>
  • 条之间的空间(由 参数设置ybar,例如ybar=1pt

这些选项的具体选择取决于文档的设置方式以及文本块的宽度。以下示例中的选择适用于articleA4 纸的标准。

您说您希望文本保持相同的大小,并且大概包括 xticklabels。使用标准时,article没有足够的空间来水平放置所有刻度标签。如果您的文档有更宽的文本块,则可能会有空间。我猜选项是要么不打印所有标签(如下所示),要么旋转标签。如果您更喜欢该选项,请取消注释带有xtick distance=1和 的行x tick label style={..}。您还需要将图例向下移动一点。

请注意,当选项列表中的值中有 时],如ylabel=[MWh],您需要将值括在括号中,否则pgfplots会将 视为选项]的右括号axis,因此其后的所有内容基本上都会被忽略。即,执行ylabel={[MWh]}

我还删除了符号坐标,当你有均匀分布的数值 x 值时,我真的不认为这里需要它们。


1pgfplots不计算整个图表(包括所有标签)的确切大小。它只是为标签分配固定大小的空间,即 45pt。因此,如果您设置width=345ptpgfplots实际上会将轴框的大小(不包括标签)单独设置为 300pt,总宽度为 300pt + 标签占用的空间量。如果您设置scale only axis,则width/height仅适用于轴框。

在此处输入图片描述

\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={.}
]
\addplot[fill=Grey] coordinates {   
(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)
};   
\addplot[fill=LightBlue] coordinates {      
(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 )      
};   
\addplot[fill=Blue] coordinates {    
(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)      
};   
\addplot[fill=DarkBlue] coordinates {          
(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}

相关内容