我从复制代码根据数据绘制图表(基本图、饼图、条形图)的指南并进行自定义(只需改变数据值)。
条形宽度太窄,导致百分比互相重叠。 因此,我增加了条形宽度 然后图表侧面的 2 条柱线跑到外面(几乎跑到一半)
如何解决这个问题?
以下是导致问题的完整源代码(图片2)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{matrix}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\title{draw_chart}
\author{Thien Thai}
\date{August 2017}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT & carD & carR \\
0--2 & 51.92 & 50.84 & 51.7 \\
2--5 & 51 & 50.53 & 50 \\
5--10 & 51.53 & 51.51 & 52.46 \\
}\mydata
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=0.7cm,
width=\textwidth,
height=.5\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={0--2,2--5,5--10},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=49.5,ymax=53,
ylabel={},
]
\addplot table[x=interval,y=carT]{\mydata};
\addplot table[x=interval,y=carD]{\mydata};
\addplot table[x=interval,y=carR]{\mydata};
\legend{Trips, Distance, Energy}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我建议bar width=.8cm
和enlarge x limits={abs=2*\pgfplotbarwidth}
。
补充说明:为 pgfplots 设置 compat:\usepackage{pgfplots} 直接后跟\pgfplotsset{compat=1.15}
(1.15 是当前版本)或\pgfplotsset{compat=newest}
。
代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=1.15}%<- added
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{matrix}
\usetikzlibrary{patterns}
\title{draw_chart}
\author{Thien Thai}
\date{August 2017}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT & carD & carR \\
0--2 & 51.92 & 50.84 & 51.7 \\
2--5 & 51 & 50.53 & 50 \\
5--10 & 51.53 & 51.51 & 52.46 \\
}\mydata
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=0.8cm,%<- changed
width=\textwidth,
height=.5\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={0--2,2--5,5--10},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=49.5,ymax=53,
ylabel={},
enlarge x limits={abs=2*\pgfplotbarwidth}
]
\addplot table[x=interval,y=carT]{\mydata};
\addplot table[x=interval,y=carD]{\mydata};
\addplot table[x=interval,y=carR]{\mydata};
\legend{Trips, Distance, Energy}
\end{axis}
\end{tikzpicture}
\end{document}
也许你想添加
nodes near coords style={/pgf/number format/.cd,fixed,fixed zerofill,precision=2},
到axis
选项获取
或者precision=1
: