11

I was wondering if there was a way to get the line plots "Unseamed" and "Requirement" to span the whole width of the graph not just from the centre points of the first and last x axis labels. Is there a way to create invisible x axis labels to achieve this?

\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}
  • Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}) or hit Ctrl+K. – Claudio Fiandrino Apr 2 '13 at 13:48
  • Is it 3.430 for both of them ? I mean both of them are horizontal lines? – percusse Apr 2 '13 at 13:52
  • Percusse - I would like the two horizontal line plots to span the whole width of the graph (i.e. touching the y axis). – Katie Apr 2 '13 at 14:19
11

Here is a somewhat roundabout way of achieving this. First add \coordinates at (Plain,3.43) and (Plain, 10.857), and in the lower corners with the rel axis cs coordinate system, then draw lines using these coordinate as reference. Legend entries are added with \addlegendimage.

Note that units should not generally be written in italics, so a change there is recommended. You could do $\mathrm{N}\,\mathrm{mm}^{-1}$, but a better way of handling units is the siunitx package, which lets you write \si{\newton\per\square\milli\metre}. In addition, I can mention the units library of pgfplots, the features of which can be combined with siunitx as in the code below (taken from the pgfplots manual).

enter image description here

\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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.