pgfplots-如何在带有符号 x 轴标签的条形图中创建水平线图

pgfplots-如何在带有符号 x 轴标签的条形图中创建水平线图

我想知道是否有办法让“无接缝”和“要求”线图跨越整个图形宽度,而不仅仅是从第一个和最后一个 x 轴标签的中心点开始。是否有办法创建不可见的 x 轴标签来实现这一点?

\documentclass[a4paper,10pt]{report}

\usepackage{pgfplots}
\usetikzlibrary{patterns}


\begin{document}

\begin{figure}
 \centering

\begin{tikzpicture}
\begin{axis}[width=11cm,
ybar,
enlargelimits=0.3,legend style={at={(0.98,0.98)},
cells={anchor=west}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength{,} $N\,mm^{-1}$},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]

\addplot [draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};

\addplot[black,sharp plot]
coordinates {(Plain,10.857) (Double lap,10.857)}
;

\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};

\addplot[black,sharp plot,dashed]
coordinates {(Plain,3.430) (Double lap,3.430)}
;

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

答案1

实现这一点的方法有点迂回。首先在坐标系的下角的和处添加\coordinates ,然后使用这些坐标作为参考绘制线条。图例条目用 来添加。(Plain,3.43)(Plain, 10.857)rel axis cs\addlegendimage

请注意,单位通常不应以斜体书写,因此建议进行更改。您可以这样做$\mathrm{N}\,\mathrm{mm}^{-1}$,但处理单位的更好方法是使用siunitx包,它允许您编写\si{\newton\per\square\milli\metre}。此外,我可以提到unitspgfplots,其功能可以与结合使用,siunitx如下面的代码所示(取自手册pgfplots)。

在此处输入图片描述

\documentclass[border=2mm,tikz]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{units}
\pgfplotsset{unit code/.code 2 args={\si{#1#2}}}
\usetikzlibrary{patterns}
\usepackage{siunitx}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=11cm,
ybar,
y unit=\newton\per\square\milli\metre,
enlargelimits=0.3,
legend style={
at={(0.98,0.98)},
cells={anchor=west},
column 3/.style={width=2cm}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]

\addplot [draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};

\addlegendimage{line legend,black,sharp plot}

\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}] 
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};

\addlegendimage{line legend,black,sharp plot,dashed}


\coordinate (A) at (axis cs:Plain,3.430);
\coordinate (B) at (axis cs:Plain,10.857);
\coordinate (O1) at (rel axis cs:0,0);
\coordinate (O2) at (rel axis cs:1,0);

\draw [black,sharp plot,dashed] (A -| O1) -- (A -| O2);
\draw [black,sharp plot] (B -| O1) -- (B -| O2);


\end{axis}
\end{tikzpicture}

\end{document}

相关内容