\documentclass{article}
\usepackage[adobefonts]{ctex} %支持中文就靠这一行了
\usepackage{fontspec}
\usepackage{tikz}
\usepackage[active,xetex,tightpage]{preview} %这一行有改动,将pdftex换成xetex
\PreviewEnvironment[]{tikzpicture}
\PreviewEnvironment[]{pgfpicture}
\DeclareSymbolFont{symbolsb}{OMS}{cmsy}{m}{n}
\SetSymbolFont{symbolsb}{bold}{OMS}{cmsy}{b}{n}
\DeclareSymbolFontAlphabet{\mathcal}{symbolsb}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\newcommand\YaHei{\fontspec{微软雅黑}}
%
% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}
\pgfplotsset
{
tick label style = {font=\YaHei\bfseries},
xticklabel style = {font=\YaHei\bfseries},
legend style = {font=\YaHei\bfseries,,cells={anchor=west}},
}
\begin{document}
% ybar控制同一组内bar之间的微小间隔
% ymin控制Y轴的最小数字,ymax控制Y轴的最大数字
\begin{tikzpicture}
\begin{axis}[
major x tick style = transparent,
enlarge x limits = false,
width = 0.85*\textwidth,
height = 8cm,
ybar = 5*\pgflinewidth,
bar width = 24pt,
ymajorgrids = true,
title = {\YaHei\bfseries 苹果手机销售情况},
ymin = 0,
ymax = 100,
xtick = data,
symbolic x coords = {2006,2007,2008,2009,2010,2011,2012},
scaled y ticks = false,
enlarge x limits = 0.25,
legend cell align = left,
legend pos = outer north east,
nodes near coords,% 在柱子顶部显示实际数字
tick align = outside,
bar shift = 0pt,
]
\legend{亿元}
\addplot[style={bblue,fill=bblue,mark=none}]
coordinates {(2006, 10)(2007, 60)(2008, 70)(2009, 88)(2010, 44)(2011, 47)(2012, 54)};
\end{axis}
\end{tikzpicture}
\end{document}
结果看起来不太好看。截图如下:
如何禁用第一个小节之前和最后一个小节之后的空间?
答案1
选项值enlarge x limits
决定了数据边界左右两侧的额外空间。OP 使用的这个值0.25
对于他的请求来说太大了,因此0.1
将其减少到一个比较合适的量。
\documentclass{article}
%\usepackage[adobefonts]{ctex} %支持中文就靠这一行了
\usepackage{fontspec}
\usepackage{tikz}
\usepackage[active,xetex,tightpage]{preview} %这一行有改动,将pdftex换成xetex
\PreviewEnvironment[]{tikzpicture}
\PreviewEnvironment[]{pgfpicture}
%\DeclareSymbolFont{symbolsb}{OMS}{cmsy}{m}{n}
%\SetSymbolFont{symbolsb}{bold}{OMS}{cmsy}{b}{n}
%\DeclareSymbolFontAlphabet{\mathcal}{symbolsb}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\newcommand\YaHei{}%\fontspec{微软雅黑}}
%
% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}
\pgfplotsset
{
tick label style = {font=\YaHei\bfseries},
xticklabel style = {font=\YaHei\bfseries},
legend style = {font=\YaHei\bfseries,,cells={anchor=west}},
}
\begin{document}
% ybar控制同一组内bar之间的微小间隔
% ymin控制Y轴的最小数字,ymax控制Y轴的最大数字
\begin{tikzpicture}
\begin{axis}[
major x tick style = transparent,
enlarge x limits = false,
width = 0.85*\textwidth,
height = 8cm,
ybar = 5*\pgflinewidth,
bar width = 24pt,
ymajorgrids = true,
title = {\YaHei\bfseries 苹果手机销售情况},
ymin = 0,
ymax = 100,
xtick = data,
symbolic x coords = {2006,2007,2008,2009,2010,2011,2012},
scaled y ticks = false,
scale only axis,
enlarge x limits = 0.1,
legend cell align = left,
legend pos = outer north east,
nodes near coords,% 在柱子顶部显示实际数字
tick align = outside,
bar shift = 0pt,
]
\legend{亿元}
\addplot[style={bblue,fill=bblue,mark=none}]
coordinates {(2006, 10)(2007, 60)(2008, 70)(2009, 88)(2010, 44)(2011, 47)(2012, 54)};
\end{axis}
\end{tikzpicture}
\end{document}