
Me preguntaba si había una manera de hacer que los gráficos de líneas "Sin unir" y "Requisito" abarquen todo el ancho del gráfico, no solo desde los puntos centrales de la primera y última etiqueta del eje x. ¿Hay alguna manera de crear etiquetas invisibles del eje x para lograr esto?
\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}
Respuesta1
He aquí una forma un tanto indirecta de lograrlo. Primero agregue \coordinate
s en (Plain,3.43)
y (Plain, 10.857)
, y en las esquinas inferiores con el rel axis cs
sistema de coordenadas, luego dibuje líneas usando estas coordenadas como referencia. Las entradas de leyenda se agregan con \addlegendimage
.
Tenga en cuenta que las unidades generalmente no deben escribirse en cursiva, por lo que se recomienda cambiarlas. Podrías hacerlo $\mathrm{N}\,\mathrm{mm}^{-1}$
, pero una mejor forma de manejar unidades es el siunitx
paquete, que te permite escribir \si{\newton\per\square\milli\metre}
. Además, puedo mencionar la units
biblioteca de pgfplots
, cuyas características se pueden combinar siunitx
como en el código siguiente (tomado del pgfplots
manual).
\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}