我正在制作一个图表,其中包含一组类别的最小值、最大值和平均值。我的灵感来自图片 2。在上一个问题问,我已经能够做出图 1 中所示的效果。
有没有办法将误差线的末端改为其他形状(正方形、圆形),就像图 2 中那样?
有没有办法添加标签来显示哪些值是最大值、最小值和平均值,如图 2 所示?(“Landsgjennomsnitt” = 平均值,“... høyest andel” = 最大值,等等)
有没有办法在每个条形的最大值和最小值旁边添加小文本?如图 3 所示。
\pgfplotstableread{
x y y-max y-min
{Kategori 1} 9.2 7.3 5.3
{Kategori 2} 11.1 4.3 4.8
{Kategori 3} 12.2 3.5 3.3
{Kategori 4} 12.3 3.7 3.5
{Kategori 5} 21.0 5.2 3.7
}{\differanser}
\begin{tikzpicture}[scale=1.3]
\begin{axis} [
width = 0.5*\textwidth,
height = 8cm,
symbolic x coords={{Kategori 1},{Kategori 2},{Kategori 3},{Kategori 4},{Kategori 5}},
minor ytick={5,10,15,20,25},
yminorgrids,
xtick=data,
ticklabel style = {font=\tiny},
x tick label style={rotate=45,anchor=east}
]
\addplot+[blue, very thick, forget plot,only marks]
plot[very thick, error bars/.cd, y dir=plus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-max}] {\differanser};
\addplot+[red, very thick, only marks,xticklabels=\empty]
plot[very thick, error bars/.cd, y dir=minus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-min}] {\differanser};
\end{axis}
\end{tikzpicture}
答案1
由于您似乎还想要图例条目,最简单的选择可能是添加相应的图。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread{
x y y-max y-min
{Kategori 1} 9.2 7.3 5.3
{Kategori 2} 11.1 4.3 4.8
{Kategori 3} 12.2 3.5 3.3
{Kategori 4} 12.3 3.7 3.5
{Kategori 5} 21.0 5.2 3.7
}{\differanser}
\begin{tikzpicture}[scale=1.3]
\begin{axis} [
width = 0.5*\textwidth,
height = 8cm,
symbolic x coords={{Kategori 1},{Kategori 2},{Kategori 3},{Kategori 4},{Kategori 5}},
minor ytick={5,10,15,20,25},
yminorgrids,
xtick=data,
ticklabel style = {font=\tiny},
x tick label style={rotate=45,anchor=east},
legend style={at={(0.05,0.95)},anchor=north west,cells={anchor=west},column
sep=1ex}
]
\addplot+[blue, very thick, forget plot,only marks,forget plot]
plot[very thick, error bars/.cd, y dir=plus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-max}] {\differanser};
\addplot+[red, very thick, only marks,xticklabels=\empty,forget plot]
plot[very thick, error bars/.cd, y dir=minus, y explicit]
table[x=x,y=y,y error expr=\thisrow{y-min}] {\differanser};
\addplot[only marks,mark=*,mark options={fill=blue,draw=red,very thick}]
table[x=x,y expr=\thisrow{y}] {\differanser};
\addlegendentry{average}
\addplot[only marks,mark=square*,color=blue]
table[x=x,y expr=\thisrow{y}+\thisrow{y-max}] {\differanser};
\addlegendentry{max}
\addplot[only marks,mark=square*,color=red]
table[x=x,y expr=\thisrow{y}-\thisrow{y-min}] {\differanser};
\addlegendentry{min}
\end{axis}
\end{tikzpicture}
\end{document}