我对该图有几个疑问:
- 我正在尝试创建一个模板图,可以多次使用不同数量的条形图。为了简化操作,我想定义固定的条形宽度和条形之间的固定间距。x 轴的长度应根据条形图的数量而变化。y 轴在每个图中的高度应相同。
- 有没有办法简化 \node [above] at (axis cs: A,1) {100\%}; 表达式,这样我就不必手动输入值
- 我希望 x 标签能够自动调整到 x 轴的长度
- 我想删除 y 轴箭头顶部的小线
提前感谢 Andreas
\documentclass[a4paper,twoside,11pt]{scrreprt}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={/pgf/number format/fixed},
ybar=5pt,
bar width=12pt,
x=4cm,
ymin=0,
xtick=data,
xlabel=a label that is longer than the x-axis and needs an automatic break,
enlarge x limits=10,
symbolic x coords={A, B},
point meta={y*100}, %y-Werte mal 100 für Prozent
yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
axis lines*=left,
axis x line*=bottom, axis y line=left,
enlarge x limits=auto,
clip=false
]
\addplot [draw=black, fill=white, error bars/.cd, y dir=both, y explicit] coordinates{(A,1) (B,.155)+-(.038,.038)};
\node [above] at (axis cs: A,1) {100\%};
\node (a) [above] at (axis cs: B,.193) {15,5\%};
\node [above=-0.2cm of a] {*};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
- 要获取条形中心之间的固定距离,请设置
x=<distance>
。要设置条形的宽度,请设置bar width=<distance>
。要获取第一个条形与绘图左边缘之间以及最后一个条形与绘图右边缘之间的固定空间量,请设置enlarge x limits={abs=<distance>}
。 - 要标记条形,请设置
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\,\%},
- 我一下子想不出一个优雅的方法来做到这一点。也许你可以为此提出一个新问题?
- 设置
axis lines*=left
(带星号)以关闭箭头。
\documentclass[a4paper,twoside,11pt]{scrreprt}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\begin{document}
\makeatletter
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=1cm, % Width of the bar
x=2cm, % Distance between the centers of the bars
enlarge x limits={abs=1cm}, % The distance between the center of the first bar and the left edge
enlarge y limits=false,
ymin=0,
xtick=data,
xlabel=a label that is longer than the x-axis and needs an automatic break,
symbolic x coords={A, B},
point meta={y*100}, %y-Werte mal 100 für Prozent
yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
axis lines*=left,
clip=false
]
\addplot [
draw=black,
fill=white,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\,\%},
error bars/.cd,
y dir=both,
y explicit
] coordinates{(A,1) (B,.155)+-(.038,.038)};
\pgfplotsextra{
\pgfmathfloattofixed{\pgfplots@data@xmax}
\pgfplotsset{xlabel style=1cm}
}
\end{axis}
\end{tikzpicture}
\end{document}