Represent data with a status bar plot

1 次查看(过去 30 天)
Hello everyone!
I'm looking for a way to represent my data in the form of a status bar, the colour of which depends on the value.
The end result should look something like this
Does anyone know the function that plots something like this?

采纳的回答

Voss
Voss 2025-3-4
t = load('mydata.mat').mydata;
X = repmat(double(t.Time(:)),1,2);
C = repmat(double(t.Data(:)),1,2);
Y = zeros(size(X))+[0 1];
figure('Position',[1 1 900 600])
tiledlayout(2,2)
nexttile
plot(t.Time,t.Data)
xlim(t.Time([1 end]))
title(sprintf('line plot:\nentire timeseries'))
nexttile
plot(t.Time,t.Data)
xlim([0 40])
title(sprintf('line plot:\nzoomed to beginning'))
nexttile
surface(X,Y,C,'EdgeColor','none')
xlim(X([1 end],1))
ylim(Y(1)+[-0.5 1.5])
colorbar
title(sprintf('status-bar plot:\nentire timeseries'))
nexttile
surface(X,Y,C,'EdgeColor','none')
xlim([0 40])
ylim(Y(1)+[-0.5 1.5])
colorbar
title(sprintf('status-bar plot:\nzoomed to beginning'))
  4 个评论
Voss
Voss 2025-3-5
Here's one approach you can use to add the enumerated data (text objects):
t = load('mydata.mat').mydata;
X = repmat(double(t.Time(:)),1,2);
C = repmat(double(t.Data(:)),1,2);
Z = zeros(size(X));
Y = Z+[0 1];
[G,GID] = findgroups(t.Data);
NG = numel(GID);
XT = zeros(NG,1);
for ii = 1:NG
XT(ii) = mean(t.Time(G == ii));
end
YT = (Y(1,1)+Y(1,2))/2*ones(NG,1);
ST = string(GID);
args = {'HorizontalAlignment','center','Color','w','FontSize',14};
figure('Position',[1 1 900 600],'Color',[0.94,0.94,0.94])
tiledlayout(2,1)
nexttile
surface(X,Y,Z,C,'EdgeColor','none')
text(XT,YT,ST,args{:})
xlim(X([1 end],1))
ylim(Y(1,:)+[-0.5 0.5])
colorbar
title(sprintf('status-bar plot:\nentire timeseries'))
nexttile
surface(X,Y,Z,C,'EdgeColor','none')
text(XT,YT,ST,args{:})
xlim([0 40])
ylim(Y(1,:)+[-0.5 0.5])
colorbar
title(sprintf('status-bar plot:\nzoomed to beginning'))
Nicolas
Nicolas 2025-3-6
Great idea ! Here is the final result after including some text :
t = load('mydata.mat').mydata;
X = repmat(double(t.Time(:)),1,2);
C = repmat(double(t.Data(:)),1,2);
Z = zeros(size(X));
Y = Z+[0 1];
[G,GID] = findgroups(t.Data);
NG = numel(GID);
XT = zeros(NG,1);
ST_text = ["0 - a";"1 - b";"2 - c";"3 - d";"4 - e";"5 - f";"6 - g";"7 - h";"8 - i";"9 - j";"10 - k";"11 - l";"12 - m";"13 - n"; "14 - o"];
for ii = 1:NG
XT(ii) = mean(t.Time(G == ii));
enum_name(ii) = ST_text(GID(ii)+1);
end
YT = (Y(1,1)+Y(1,2))/2*ones(NG,1);
ST = enum_name;
args = {'HorizontalAlignment','center','Color','w','FontSize',14,'Rotation',90};
figure1 = figure('Position',[1 1 900 600],'Color',[0.94,0.94,0.94]);
surface(X,Y,Z,C,'EdgeColor','none')
text(XT,YT,ST,args{:})
xlim(X([1 end],1))
ylim(Y(1,:)+[-0.1 0.1])
clim([0 14])
colorbar('Ticks',[0 1 2 3 4 5 6 7 8 9 10 11 12 13],...
'TickLabels',{'0 - a','1 - b','2 - c','3 - d','4 - e','5 - f','6 - g','7 - h','8 - i','9 - j','10 - k - l','12 - m','13 - n', '14 - o'},...
'Location','southoutside');
(I've used a different data run, to make it easier to read)
Thank you again @Voss for your support !

请先登录,再进行评论。

更多回答(1 个)

Aquatris
Aquatris 2025-3-4
You can use bar() function with 'stacked' argument;
y = [30 50 10 10];
barh(1,y,"stacked");
  2 个评论
Nicolas
Nicolas 2025-3-4
Hello @Aquatris and thank you for your reply!
Unfortunately I omitted some details in my request ... I work with timeseries, so I can't really use this method, I'd first have to determine the value groups (1,2,3 etc ...) before being able to display them in stacked bar.
Aquatris
Aquatris 2025-3-4
I think you should provide your data and show exactly what you want.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by