我的图形总是偏离中心,因为右轴比左轴更靠近图形。我每次都要手动将右轴延长一个特定的量吗?
\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
%width = 0.85*\textwidth,
%height = 8cm,
%major x tick style = transparent,
ybar,%=2*\pgflinewidth,
bar width=20pt,
%ymajorgrids = true,
ylabel = {Deposition \%},
symbolic x coords={{4 ac.h$^{-1}$},{6 ac.h$^{-1}$}},
xtick = {{4 ac.h$^{-1}$},{6 ac.h$^{-1}$}},
ymax=80,
scaled y ticks = false,
enlarge x limits=0.25,
ymin=0,
legend cell align=left,
legend style={
at={(1,1.05)},
anchor=south east,
column sep=1ex
}
]
\addplot [fill=black, draw=black,bar shift=3pt] coordinates {({4 ac.h$^{-1}$},61.66779959)}; %\addlegendentry{4 ac.h$^{-1}$}
\addplot [fill=white, draw=black,bar shift=-3pt] coordinates {({6 ac.h$^{-1}$},65.77432124)};
%\addplot [fill=white, draw=black, bar shift=0.5ex] table [x=X, y=6ACH] {\datatable}; \addlegendentry{6 ac.h$^{-1}$}
\end{axis}
\end{tikzpicture}
\end{document}
这似乎不适用于独立文档类pgfplots 和图形居中
这是临时的:将 TikZ 图片置于某个区域的中心 Excel:5 秒,但没有 Tex 格式和错误字体。
编辑
轴标签被截断,并成为 ytick 的一部分
\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\begin{document}
\definecolor{mycolor1}{rgb}{0.0416666666666667,0,0}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[%
width=6.17578125in,
height=2.23276041666667in,
area legend,
scale only axis,
xmin=0.5,
xmax=5.5,
xtick={1,2,3,4,5},
xticklabels={Equipment,Patient,{Hygiene products},Near-bed,Far-bed},
ymin=0,
ymax=0.35,
ytick={ 0, 0.1, 0.2, 0.3, 0.4},
ylabel={Probability density},yticklabel style={/pgf/number format/fixed},
legend style={at={(0.143572984749455,0.733206590621038)},anchor=south west,draw=black,fill=white,legend cell align=left}
]
\addplot[ybar,bar width=0.282321428571429in,bar shift=-0.176450892857143in,fill=mycolor1,draw=black] plot coordinates{(1,0.163793103)
(2,0.164367816)
(3,0.141954023)
(4,0.279310345)
(5,0.250574713)};
\addlegendentry{Smith et al.};
\addplot [
color=black,
solid,
forget plot
]
table[row sep=crcr]{
0.5 0\\
5.5 0\\
};
\addplot[ybar,bar width=0.282321428571429in,bar shift=0.176450892857143in,fill=white,draw=black] plot coordinates{(1,0.165289256)
(2,0.185950413)
(3,0.157024793)
(4,0.285123967)
(5,0.20661157)};
\addlegendentry{Adjusted YAB};
\end{axis}
\end{tikzpicture}%
\end{document}
答案1
如果您使用\begin{tikzpicture}[trim axis left, trim axis right]
,轴将正确居中。
答案2
我想到的是简单地添加一个具有边界框大小的节点,其中inner xsep
,同时使用轴作为边界框,overlay
并且ylabel
和yticklabel
:
\usepackage{pgfplots}
\usetikzlibrary{fit}
\begin{tikzpicture}
\begin{axis}[
use as bounding box,
ylabel style={overlay},
yticklabel style={overlay},
% ...
]
% ...
\end{axis}
\node[
draw=none,
fit=(current bounding box.north west)
(current bounding box.south east),
inner xsep=5em % adjust as needed
] at (current bounding box.center) {};
\end{tikzpicture}
以此题为例,结果如下:
当将其作为独立文件包含在另一个 TeX 文件中时,这也有效。