Tikz 범례 항목 이미지-잘못된 선 각도

Aug 21 2020

임계 값을 표시하는 하나의 수평선이있는 ybar tikzpicture가 있습니다.

불행히도이 선은 수평이 아니라 범례에서 대각선으로 나타납니다.

\ addlegendimage를 사용하여 몇 가지 솔루션을 시도했지만 간단한 수평선을 얻지 못했습니다.

아래에서 내 그래프와 라텍스 코드를 찾을 수 있습니다. 나는 그것이 명확하기를 바랍니다.

누군가 나를 도울 수 있습니까?

궁금한 점이 있으시면 부담없이 문의 해주세요!

많은 감사드립니다!

Matze

\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}

답변

2 AndréC Aug 21 2020 at 21:29

나는 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}