问题:线在给定的坐标处停止,不遵守 ymax。
\documentclass[a4paper,10pt]{report}
\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
\usepackage[table,usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
width=\textwidth,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,enlarge y limits={upper,value=1},
height=8cm,
enlargelimits=0.25,
ylabel={Uptime \%},
symbolic x coords={Mar '12,Apr '12,May '12},
xtick=data,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
]
\addplot[draw=blue,fill=blue!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=Salmon,fill=Salmon!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=yellow,fill=yellow!40!white] coordinates {(Mar '12,99.86) (Apr '12,99.47) (May '12,99.37)};
\addplot[draw=RawSienna,fill=RawSienna!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.98)};
\addplot[draw=purple,fill=purple!40!white] coordinates {(Mar '12,99.69) (Apr '12,98.79) (May '12,97.91)};
\addplot[draw=cyan,fill=cyan!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=magenta,fill=magenta!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.96)};
\addplot[draw=LimeGreen,fill=LimeGreen!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.99)};
\addplot[draw=red,fill=red!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.99)};
\addplot[red,sharp plot] coordinates {(Mar '12,98) (May '12,98)} node[above] at (axis cs:Apr '12,98) {KPI};
\end{axis}
\end{tikzpicture}
\end{document}
现在,我想在 98% 处画一条连续的线,没有标签,但带有 KPI 标签,并且 y 轴应该在 100 处停止。这可能吗?
答案1
您可以采用以下解决方案如何在绘图中添加零线?添加红线:
\draw [red] ({rel axis cs:0,0}|-{axis cs:May '12,98}) -- ({rel axis cs:1,0}|-{axis cs:May '12,98}) node [pos=0.33, above] {KPI};
将在整个图宽上绘制一条红线,并在长度的三分之一处添加标签。请注意,x 坐标May '12
实际上在这里没有任何用处,但您仍需要提供一个有效值。
ymax
不遵守您的 的原因是您调用了enlargelimits=0.25
,这会覆盖ymax
。此外,您还设置了enlarge y limits={upper,value=1}
,这也会提高 y 上限。将这些选项替换为enlarge x limits=0.25
,您的 y 上限就会得到遵守。在这种情况下,您可能希望设置x axis lines*=left
,这样只会绘制底部 x 轴线(*
会关闭原本要使用的箭头尖端)。
\documentclass[a4paper,10pt]{report}
\usepackage[left=1.5cm,top=2cm,right=1.5cm,bottom=1.5cm]{geometry}
\usepackage[table,usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
width=\textwidth,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,
height=8cm,
enlarge x limits=0.25,
axis x line*=left,
ylabel={Uptime \%},
symbolic x coords={Mar '12,Apr '12,May '12},
xtick=data,
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
]
\addplot[draw=blue,fill=blue!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=Salmon,fill=Salmon!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=yellow,fill=yellow!40!white] coordinates {(Mar '12,99.86) (Apr '12,99.47) (May '12,99.37)};
\addplot[draw=RawSienna,fill=RawSienna!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.98)};
\addplot[draw=purple,fill=purple!40!white] coordinates {(Mar '12,99.69) (Apr '12,98.79) (May '12,97.91)};
\addplot[draw=cyan,fill=cyan!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,100)};
\addplot[draw=magenta,fill=magenta!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.96)};
\addplot[draw=LimeGreen,fill=LimeGreen!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.99)};
\addplot[draw=red,fill=red!40!white] coordinates {(Mar '12,100) (Apr '12,100) (May '12,99.99)};
\draw [red] ({rel axis cs:0,0}|-{axis cs:May '12,98}) -- ({rel axis cs:1,0}|-{axis cs:May '12,98}) node [pos=0.33, above] {KPI};
\end{axis}
\end{tikzpicture}
\end{document}