How to draw a histogram on FreeMat?

1,121 views
Skip to first unread message

Hsu

unread,
Feb 24, 2010, 12:57:03 AM2/24/10
to freemat-devel
Dear All

How to draw a histogram on FreeMat?

Does the HIST function work for drawing a histogram?

Thanks!

Timothy Cyders

unread,
Feb 24, 2010, 7:36:01 AM2/24/10
to freema...@googlegroups.com
We do have a histogram function, hist(). You can either just give it your y vector like so: [n,x] = hist(y) or give it your y vector and a number of elements to evaluate on the x-axis of the histogram like so: [n,x] = hist(y,20). Far as I know, we still don't have a bar-plotting routine, but here's a quick and dirty one I just mocked up. Just use the command bar(x,n) and you should get a bar graph of your histogram plot. If you want to simply plot a line with your histogram values, you can do a simple plot(x,n).

TJ

function bar(x,y)


xspacing = x(2)-x(1); % assuming monotonic x vector

barwidth = xspacing*0.5;

lx = length(x);

rightbarx = zeros(lx,2);

leftbarx = zeros(lx,2);

topbarx = zeros(lx,2);

rightbary = zeros(lx,2);

leftbary = zeros(lx,2);

topbary = zeros(lx,2);


for i = 1:lx;

    leftbarx(i,:) = [(x(i)-0.5*barwidth) (x(i)-0.5*barwidth)];

    rightbarx(i,:) = [(x(i)+0.5*barwidth) (x(i)+0.5*barwidth)];

    topbarx(i,:) = [(x(i)-0.5*barwidth) (x(i)+0.5*barwidth)];

    leftbary(i,:) = [0 y(i)];

    rightbary(i,:) = [0 y(i)];

    topbary(i,:) = [y(i) y(i)];

end


figure(); hold on;


for i = 1:lx

    plot(leftbarx(i,:),leftbary(i,:),'-k',rightbarx(i,:),rightbary(i,:),'-k',topbarx(i,:),topbary(i,:),'-k')

end



--
You received this message because you are subscribed to the Google Groups "freemat-devel" group.
To post to this group, send email to freema...@googlegroups.com.
To unsubscribe from this group, send email to freemat-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/freemat-devel?hl=en.


Timothy Cyders

unread,
Feb 24, 2010, 7:50:16 AM2/24/10
to freema...@googlegroups.com
This does bring up an issue with the patch function - it doesn't seem to work at all. Not in the 4.0.1 build, not in the current SVN build. Can anyone confirm this?

TJ

Eugene

unread,
Feb 25, 2010, 2:41:03 AM2/25/10
to freemat-devel
patch should work. There was a typo in the documentation and also in
matlab patch implementation doesn't exactly follow its own
documentation. I made FreeMat implementation match closer in the
latest revisions. There are some corner cases for which our patch
probably doesn't work correctly (e.g. 4 vertices in space not in the
same plane), or some combination of input params which won't work.

In FreeMat hist uses patch, by the way. Also, there is bar
implementation in the hist function (borrowed from octave):

function h=bar( x, y, width, varargin )

x=x(:);
y=y(:);
if (length(x) ~= length(y))
error('cannot handle this case');
end

df = width*diff( x )/2;
df(end+1) = df(end);
x_r=x(1:end)-df;
x_l=x(1:end)+df;

n = length(x);
vertices = [ x_r zeros(size(x_r)) ones(size(x_r)) ;...
x_r y ones(size(x_r)) ;...
x_l y ones(size(x_r)) ;...
x_l zeros(size(x_l)) ones(size(x_r))];

ind=(1:n)';
faces = [ind ind+n ind+2*n ind+3*n];
if ~ishold
clf
end
h=patch('Faces',faces,'Vertices',vertices,'FaceColor',[0 0 1]);


Eugene

unread,
Feb 25, 2010, 2:42:05 AM2/25/10
to freemat-devel
hist(rand(1,100))

Timothy Cyders

unread,
Feb 25, 2010, 7:01:36 AM2/25/10
to freema...@googlegroups.com
Huh, for some reason that didn't work at all when I did it yesterday and works fine today. I must've been putting in typos of my own. At any rate, looks good! Apologies for the confusion.

TJ

On Thu, Feb 25, 2010 at 2:42 AM, Eugene <gen...@gmail.com> wrote:
hist(rand(1,100))

Timothy Cyders

unread,
Feb 25, 2010, 7:17:17 AM2/25/10
to freema...@googlegroups.com
Eugene,

I had one issue with hist() in that it uses two functions native to octave that neither we nor MATLAB have - columns() and rows(). These are simply equivalent to size(y,2) and size(y,1), respectively. If you try to run hist(rand(1,100),30) for example, to name 30 bins instead of the default 10, you'll get an error showing there is no such variable as columns. I've made the changes to my local m-file, let me know if you think I should update in SVN.

TJ

Eugene

unread,
Feb 25, 2010, 2:08:05 PM2/25/10
to freemat-devel
I fixed columns problem yesterday. Fixed rows today.

Thanks,
Eugene

Reply all
Reply to author
Forward
0 new messages