Everyone,
Is there code to make a similar plot in Matlab as the following (from excel) with categorical x-axis?
I'm fairly amateur with matlab so, please be explicit.
I make it for the bars using categorical, so i get four categories of 5cm, 10, 20 and 40 but cant get the line like that?
Cheers.

2 Kommentare

Walter Roberson
Walter Roberson am 6 Jun. 2019
If the 5, 10, 20, 40 are intended to represent x locations, then No, you have a problem of duplicate x values.
bar() can accept categorical data for the x axis. It will use the value associated with the category as the x displacement.
George Korovesis
George Korovesis am 6 Jun. 2019
Bearbeitet: George Korovesis am 6 Jun. 2019
Thank you.
I make the bar chart as such:
x = categorical({'5cm','10cm','20cm','40cm'});
x = reordercats(x,{'5cm','10cm','20cm','40cm'});
y = ([0.6 0.8 1.0; 0.6 0.8 1.0; 0.6 0.8 1.0; 0.6 0.8 1.0]);
bar(x,y,'c');
But that's the most I can. Can't get the line to go through each bar (it goes through the category 5cm) if that makes sense.
I tried to use secondary axis but again I can't make it works.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Jun. 2019

0 Stimmen

You would have to do something like
b = bar(double(x), y, 'c');
xticklabels( {'5cm','10cm','20cm','40cm'} );
drawnow() %important to do before getting the children
xpos = cell2mat(arrayfun(@(S) unique(S.NodeChildren(1).VertexData(1,:)).', b, 'uniform', 0));
Now xpos(:,1) will be the coordinates of the left and right edges of the first bar in each group. So you could for example,
xc = squeeze(mean(reshape(xpos,2,[],size(xpos,2))));
and that would be number of groups x bars per group of x coordinates of the centers of bars.
hold on
lp = plot(reshape(xc.',[],1),reshape(y.',[],1));
hold off

1 Kommentar

George Korovesis
George Korovesis am 7 Jun. 2019
That's amazing!!!
Thanks for that. I don't even understand the code but it works (children???)!
Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by