Better way to autoscale x axis a histogram
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to "autoscale" the x-axis of a histogram on an axes component. I think I have done it using for loops but was wondering if there was a more elegant way to do it.
I first get the histogram data from the axes.
axes(handles.axes2);
barObj= findobj(gca, 'type', 'bar');
x=get(barObj,'XData')';
y=get(barObj,'YData')';
Determine max x & max y
mxy=max(y);
mxx=max(x);
Find index of max Y value so can get its "x" value. Also define a level value, below which I will declare the "bounds" of the histogram to then pass to xlim.
idx = find(y==mxy);
level=0.01*mxy
Get upper limit of X axis: The idea is to count x from the x location where Y is max and count outwards until 3 consecutive Y values less than level are obtained.
%Get upper limit
for i=idx:max(x)-3
i=i+1;
y1=y(i);
y2=y(i+1);
y3=y(i+2);
if(y1&y2&y3<level)
indexH=i;
break
end
end
Likewise for the lower X value bound:
%Get lower limit
indexL=0; %set incase not found
for i=0:idx
i=i+1;
y1=y(i);
y2=y(i+1);
y3=y(i+2);
if(y1&y2&y3>level)
indexL=i;
break
end
end
Now rescale the x -axis
xlim([indexL indexH])
Here is the original histogram
and what I am trying to achieve (more elegantly)
0 comentarios
Respuestas (1)
Kirby Fears
el 24 de Nov. de 2015
After plotting:
axis tight
axis 'auto y'
axis tight makes both x and y axes fit the min/max values of the axis. Then 'auto y' reverts the y axis scale back to the standard setting.
2 comentarios
Kirby Fears
el 25 de Nov. de 2015
Editada: Kirby Fears
el 25 de Nov. de 2015
Have you considered excluding outliers before plotting? It makes sense to apply your "selection" logic to the data itself instead of the axis boundaries:
"Get upper limit of X axis: The idea is to count x from the x location where Y is max and count outwards until 3 consecutive Y values less than level are obtained."
Ver también
Categorías
Más información sobre Histograms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!