我曾尝试调整所有的长度,但我无法绘制图形的全长。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT & carD \\
1 & 1.2 & 0.1 \\
2 & 12.8 & 3.8 \\
3 & 15.5 & 10.4 \\
4 & 14.0 & 17.3 \\
5 & 7.9 & 21.1 \\
6 & 3.0 & 22.3 \\
7 & 3.0 & 22.3 \\
8 & 3.0 & 22.3 \\
9 & 3.0 & 22.3 \\
10 & 3.0 & 22.3 \\}\mydata
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=.3cm,
width=\textwidth,
height=.8\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={1,2,3, 4, 5,6,7,8,9,10},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=0,ymax=35,
ylabel={\%},
]
\addplot table[x=interval,y=carT]{\mydata};
\addplot table[x=interval,y=carD]{\mydata};
\legend{Trips, Distance}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
正如主题补充Janis Lazovskis 的回答对您的代码及其图像边框命题进行小幅修改。代码中的更改以 表示% <---
。
编辑:表mydata is moved inside
tikzpicture` 并做了一些改变
\documentclass[margin=1pt]{standalone} % <---
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} % <---
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
interval carT carD
1 1.2 0.1
2 12.8 3.8
3 15.5 10.4
4 14.0 17.3
5 7.9 21.1
6 3.0 22.3
7 3.0 22.3
8 3.0 22.3
9 3.0 22.3
10 3.0 22.3
}\mydata
\begin{axis}[
width=\linewidth,
height=.8\linewidth,
ybar=0.5mm, % <--- distance between bars (shift bar)
bar width=4.4mm, % <--- width of bars
legend style={legend columns=-1,
outer sep=1mm, % <---
font=\scriptsize, % <---
anchor=north,
at={(0.5,1)},
},
nodes near coords,
nodes near coords style={font=\scriptsize, inner sep=2pt}, % <---
nodes near coords align={vertical},
ylabel={\%},
ymin=0, ymax=30, % <---
xtick=data,
scale only axis, % <---
]
\addplot table[x=interval,y=carT]{\mydata};
\addplot table[x=interval,y=carD]{\mydata};
\legend{Trips, Distance}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您可以在选项中对其进行调整standalone
,通过将代码的第一行更改为:
\documentclass[tikz,border={0 0 5pt 0}]{standalone}