with some help I was able to realize a stacked bar dagramm the way I need t to be. Only one part is missing: A marker which represents the median. Sadly, I couldnt come up with any ideas of how to add one and couldnt find any help in the internet. This picture shows my diagramm and how I want to add a marker the way it is shown. It doesnt have to be that way, a swaure or sth else would do the job too.

without marker

with marker

code to generate the plot: (breite is a excel sheet that i imported. you can use testw and delete the testw = breite to test the code)

% testw = [0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ...
%     0.3568 0.452 0.494 0.2982 0.3892 0.675 0.576 0.489 0.745 0.225 ];
testw = breite;

%Datenvorbereitung
gerundet = round(testw,1);

maximum = max(gerundet),1;
anzahl = ceil(maximum / 0.1);

werte = zeros([1 anzahl]);
sz = size(werte);
haeufigkeit = zeros(sz);

for i= 1:anzahl
    werte(i) = 0.1*i;
end

wertebereich = zeros(sz);

for i = 1:anzahl
    wertebereich(i) = 0.1;
end

%Häufigkeitsverteilung
for i = 1:10000
    if gerundet(i) <= 0.2
        haeufigkeit(1) = haeufigkeit(1) + 1
    else
        for j = 2:anzahl
            if gerundet(i) >= werte(j-1) && gerundet(i) < werte(j)
                haeufigkeit(j) = haeufigkeit(j) + 1
            end
        end
    end
end

%Grauwerte
prozentual = zeros(sz);

for i = 1:anzahl
    prozentual(i) = haeufigkeit(i)/sum(haeufigkeit)
end

grauwerte = zeros(sz);

for i = 1:anzahl
    grauwerte(i) = round(1-1*prozentual(i),3);
end

colormap gray
b = bar([wertebereich; nan(size(wertebereich))],'stacked');
% colorbar('Direction','reverse')
colorbar('Ticks',[0, 0.2, 0.4, 0.6 ,0.8, 1],...
         'TickLabels',{'100%','80%','60%','40%','20%', '0%'})

for i = 1:anzahl
     b(1,i).FaceColor = [grauwerte(i) grauwerte(i) grauwerte(i)];
     b(1,i).EdgeColor = 'none';
end

1 Answers

2
Luis Mendo On Best Solutions

If I understand correctly, just add something like this after your code:

hold on
med = median(gerundet);
plot([.5 1.5], [med med], '-', 'color', [.2 .6 1], 'linewidth', 2)

enter image description here