我有一个 ybar tikzpicture,其中有一条水平线来标记阈值。
不幸的是,在图例中这条线并不是水平线,而是一条对角线。
我尝试使用 \addlegendimage 的一些解决方案,但我没能得到一条简单的水平线。
下面你会看到我的图表和 latex 代码。我希望一切都清晰明了。
有人可以帮帮我吗?
如果有任何问题,请随时提问!
提前谢谢您!
马茨
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
title = \Large{\textbf{Überprüfung Alarmauslösung}},
legend style = {at = {(1,1)}, xshift=0.2cm, yshift=0cm, anchor=north west, nodes=right},
x tick label style={
/pgf/number format/1000 sep=},
ylabel = gemessene Spannung,
xlabel = Sensor Nr. in 4 Messungen,
ybar,
ymin = 0, ymax = 2.5,
bar width = 7pt,
xtick={1, 2, 3, 4},
xmin = 1, xmax = 4,
enlarge x limits = 0.2,
]
\addplot[blue!50, fill=blue!50]
coordinates {(1,2.02) (2,2.05) (3,2.08) (4,2.03)};
\addlegendentry{$Messung~1$}
\addplot[green!50, fill=green!50]
coordinates {(1,1.8) (2,1.85) (3,1.9) (4,1.82)};
\addlegendentry{$Messung~2$}
\addplot[red!50, fill=red!50]
coordinates {(1,1.84) (2,1.87) (3,1.91) (4,1.86)};
\addlegendentry{$Messung~3$}
\addplot[orange!50, fill=orange!50]
coordinates {(1,1.89) (2,1.88) (3,1.93) (4,1.86)};
\addlegendentry{$Messung~4$}
\addplot[red, sharp plot]
coordinates {(0,1.743) (1,1.743) (2,1.743) (3,1.743) (4,1.743) (5,1.743)};
\addlegendimage{no markers, red}
\addlegendentry{$Alarmschwelle$}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
line legend 2
我创建了一个名为下面的新风格
\pgfplotsset{/pgfplots/line legend 2/.style={
legend image code/.code={\draw[thick] (0cm,0cm)--(.25cm,0cm);},},}
/pgfplots/line legend
通过修改下面手册中给出的
\pgfplotsset{ /pgfplots/line legend/.style={ legend image code/.code={ \draw [mark repeat=2,mark phase=2,##1] plot coordinates { (0cm,0cm) (0.3cm,0cm) (0.6cm,0cm) }; }, }, }
该代码的注释为%<--
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\pgfplotsset{/pgfplots/line legend 2/.style={%<-- new style "line legend 2"
legend image code/.code={\draw[thick] (0cm,0cm)--(.25cm,0cm);},},}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[ legend pos=outer north east,
title = \Large{\textbf{Überprüfung Alarmauslösung}},
% legend style = {at = {(1,1)}, xshift=0.2cm, yshift=0cm, anchor=north west, nodes=right},
x tick label style={
/pgf/number format/1000 sep=},
ylabel = gemessene Spannung,
xlabel = Sensor Nr. in 4 Messungen,
ybar,
ymin = 0, ymax = 2.5,
bar width = 7pt,
xtick={1, 2, 3, 4},
xmin = 1, xmax = 4,
enlarge x limits = 0.2,
]
\addplot[blue!50, fill=blue!50]
coordinates {(1,2.02) (2,2.05) (3,2.08) (4,2.03)};
\addlegendentry{$Messung~1$}
\addplot[green!50, fill=green!50]
coordinates {(1,1.8) (2,1.85) (3,1.9) (4,1.82)};
\addlegendentry{$Messung~2$}
\addplot[red!50, fill=red!50]
coordinates {(1,1.84) (2,1.87) (3,1.91) (4,1.86)};
\addlegendentry{$Messung~3$}
\addplot[orange!50, fill=orange!50]
coordinates {(1,1.89) (2,1.88) (3,1.93) (4,1.86)};
\addlegendentry{$Messung~4$}
\addplot[red,line legend 2,sharp plot] %<-- lien legend 2
coordinates {(0,1.743) (5,1.743)};
% \addlegendimage{no markers,red}
\addlegendentry{$Alarmschwelle$}
\end{axis}
\end{tikzpicture}
\end{document}