
El gráfico de barras generado con el siguiente código está en el eje y, no comienza en 0 ni termina en 100. En cambio, muestra desde el mínimo hasta el máximo de mis números. ¿Cómo lo cambio para que muestre la escala de 0 a 100?
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.10,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
symbolic x coords={Test1,Test2,Test3,Test4},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {(Test1,40) (Test2,60) (Test3,30.1) (Test4,88.2)};
\addplot coordinates {(Test1,70) (Test2,89.1) (Test3,42.3) (Test4,71.2)};
\legend{Test one,Test two}
\end{axis}
\end{tikzpicture}
Respuesta1
Normalmente, solo necesitarías agregar ymax=100
más axis
opciones, pero como lo tienes, enlargelimits
también debes deshabilitarlo para el eje y, cambiando de enlargelimits
a enlarge x limits
.
Existen múltiples estrategias para evitar la superposición entre los números y las barras adyacentes; tres se ilustran en el siguiente código:
Aumentando el espacio entre las barras, agregando
bar shift=<length>
a cada una\addplot
.Aumentando el ancho de las barras.
Reducir el tamaño de fuente de los números.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
%Increase the space between bars, by adding a bar shift to each addplot:
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,
enlarge x limits=0.15,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
symbolic x coords={Test1,Test2,Test3,Test4},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot +[bar shift=8pt] coordinates {(Test1,40) (Test2,60) (Test3,30.1) (Test4,88.2)};
\addplot +[bar shift=-8pt] coordinates {(Test1,70) (Test2,89.1) (Test3,42.3) (Test4,71.2)};
\legend{Test one,Test two}
\end{axis}
\end{tikzpicture}
%Make the bars wider:
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,
enlarge x limits=0.2, % modified
bar width=18pt, %%% <-------- added
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
symbolic x coords={Test1,Test2,Test3,Test4},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {(Test1,40) (Test2,60) (Test3,30.1) (Test4,88.2)};
\addplot coordinates {(Test1,70) (Test2,89.1) (Test3,42.3) (Test4,71.2)};
\legend{Test one,Test two}
\end{axis}
\end{tikzpicture}
%Reduce the font size of the numbers:
\begin{tikzpicture}
\begin{axis}[
ybar,ymax=100,
enlarge x limits=0.2,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
symbolic x coords={Test1,Test2,Test3,Test4},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={font=\tiny}%%% <-------- added
]
\addplot coordinates {(Test1,40) (Test2,60) (Test3,30.1) (Test4,88.2)};
\addplot coordinates {(Test1,70) (Test2,89.1) (Test3,42.3) (Test4,71.2)};
\legend{Test one,Test two}
\end{axis}
\end{tikzpicture}
\end{document}