Visualise value and frequency of vector elements?

12 views (last 30 days)
Hi,
lets say I want to visualize which files take up the space on my hard drive. So I have collected the data an found the following vector which represents the whole capacity of my hdd
1 1 1 1 0 0 1 1 1 1 2 2 2 2
where 1 means this sector on my hdd is occupied by a file of type 1, 0 means unoccupied and 2 means occupied by file type 2.
Now I want to visualize this data. I'd like to represent the whole hdd as a bar of fixed size, the colour of the bar should represent the occupying file type. So at the first part of the bar is red (for type 1), than grey for type 0 then again red and the last part of it is blue for type 2.
How do I do this? I have tried stacked bars, but I didnt manage to stack them, when I'm only passing a one-dimensional vector.
Regards, Martin

Answers (4)

John D'Errico
John D'Errico on 23 Mar 2014
Personally, I'd suggest a histogram as the best thing.
S = [1 1 1 1 0 0 1 1 1 1 2 2 2 2];
By the way, I'd suggest getting a bigger hard drive if that is all it holds. :) Are you using MATLAB on a Commodore 64 perhaps?
smem = unique(S)
smem =
0 1 2
C = histc(S,smem)
C =
2 8 4
bar(smem,C)
It seems like you want a simple, single bar though. If so, then patch is an easy solution. Just create a sequence of patches in a figure of the appropriate size.
Or just do a pie chart. You can add labels to each bin of the pie chart too.
pie(C)

dpb
dpb on 23 Mar 2014
Edited: dpb on 23 Mar 2014
Same result here--Looks like a bug to me...
Workaround I found was to create a 2xN array w/ the second row set to NaN. bar won't show the second bar but you'll then need to clean up the x-axis as it will still be labeled for two bars.
set(gca,'xtick',1)
xlim([0.4 1.6])
works to center the one bar.

Matin
Matin on 23 Mar 2014
Well, I'm not only interested which filetypes in general, but also how big the 'gaps' (empty spaces' between the files are.
What Im really doing is arraning vectors of variable length in a 'chain' (one big vector), but the first index of each elements in the chain can only be 10,20,... To fill up the spaces between them there are padding elemens (0 in the hdd example). Now sometimes a vector has 11 elements, which means there will be a lot of padding. I need to see this. Thats why I thought a visualization of the chain and the elemens and padding inside would be nice.
  1 Comment
dpb
dpb on 23 Mar 2014
So what's wrong w/ the fixup to work around the (apparent) bug in bar?

Sign in to comment.


Star Strider
Star Strider on 23 Mar 2014
Edited: Star Strider on 23 Mar 2014
My suggestion:
FS = [1 1 1 1 0 0 1 1 1 1 2 2 2 2]
FV = ones(size(FS));
FZ = zeros(size(FS));
FS0 = FZ;
FS0(FS == 0) = 1;
FS1 = FZ;
FS1(FS == 1) = 1;
FS2 = FZ;
FS2(FS == 2) = 1;
figure(1)
barh(FV, 'b', 'BarWidth', 1.0)
hold on
barh(FS0, 'FaceColor',[0.9 0.9 0.9], 'BarWidth', 1.0)
barh(FS1, 'r', 'BarWidth', 1.0)
hold off
The FS0 ... FS2 lines look less than efficient. Experiment with the code and plot to get the sort of result you want.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!