Visualise value and frequency of vector elements?

9 次查看(过去 30 天)
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

回答(4 个)

John D'Errico
John D'Errico 2014-3-23
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 2014-3-23
编辑:dpb 2014-3-23
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 2014-3-23
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.

Star Strider
Star Strider 2014-3-23
编辑:Star Strider 2014-3-23
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.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by