Andy
Andy

Reputation: 50540

Limit data values displayed in MATLAB histogram

I have a vector that I want to print a histogram of of data for. This data ranges from -100 to +100. The amount of data around the outer edges is insignificant and therefore I don't want to see it. I am most interested in showing data from -20 to +20.

1.) How can I limit that window to print on my histogram?

The amount of data I have at 0 outnumbers of the amount of data I have anywhere in the dataset by a minimum of 10:1. When I print the histogram, the layout of element frequency is lost because it is outnumbered by 0.

2.) Is there a way that I can scale the number of 0 values to be three times the number of -1 entries?

I am expecting an exponential drop of this dataset (in general) and therefore three times the frequency of -1 would easily allow me to see the frequency of the other data.

Upvotes: 1

Views: 11955

Answers (4)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

Histogram has a few different methods of calling it. I strongly recommend you read the documentation on the function (doc hist)

What you are looking for is to put in a custom range in the histogram bin. It depends a bit on how many bins you want, but something like this will work.

Data=randn(1000,1)*20;
hist(Data,-20:20);

You could, if you want to, change the frequency of the binning as well. You could also change the axis so that you only focus on the range from -20 to 20, using a xaxis([-20 20]) command. You could also ignore the bin at 0, by using an yaxis and limiting the values to exclude the 0 bin. Without knowing what you want exactly, I can only give you suggestions.

Upvotes: 0

yuk
yuk

Reputation: 19870

1) You can limit the histogram range you see on the plot by just setting the X axes limits:

xlim([-20 20])

Setting bins in hist command is good, but remember thatall the values outside the bins will fall into the most left and right bin. So you will need to set the axes limits anyway.

2) If there is a big difference between values in different bins, one way is to transform values on Y axes to log scale. Unfortunately just setting Y axes to log (set(gca,'YScale','log')) does not work for bar plot. Calculate the histogram with hist or histc (depending on whether you want to specify bins centers or edges) and log2 the values:

[y, xbin] = hist(data);
bar(xbin, log2(y) ,'hist')

Upvotes: 1

jpjacobs
jpjacobs

Reputation: 9549

You could also just toss out any values outside the [-20,20] range by using

subsetData=data(abs(data)<=20)

Upvotes: 2

foglerit
foglerit

Reputation: 8269

You can use something like

binCenters = -20:5:20;
[N,X] = hist(V,binCenters);

N = N./scalingVector;
bar(X(2:end-1),N(2:end-1));

Note that the code excludes the extremes of N and X from the bar plot, since they contain the number of values smaller than -20 and larger than 20. Also, by building scalingVector appropriately, you can scale N as you please.

Upvotes: 2

Related Questions