Hello everybody,
I have a problem to plot a bar chart with a line plot. Basically, it does not plot the line (although it is not necessary, I just need the marker).
Which is the problem? Here my function:
x=["24h","48h","72h"];
y= [0.59 0.78; 0.61 0.84; 0.74 0.98];
yyaxis left
bar(x,y)
ylim([0 1])
ylabel('Y_{EtOH} [g/g]');
legend 'no adapted' 'adapted';
C= orderedcolors("reef");
colororder(C(2:3,:));
hold on
yyaxis right
names={'24h';'48h','72h'};
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
x=["24h","48h","72h"];
y2= [0.74 0.74; 0.44 0.57; 0.37 0.44];
plot(x,y2,'LineStyle',"none");
set(gca,'xtick',1:3,'xticklabel',names);

 Réponse acceptée

plot expects the inputs as numerical values.
You can plot the values directly, for which the x-values will be the corresponding indices.
y= [0.59 0.78; 0.61 0.84; 0.74 0.98];
bar(y)
ylim([0 1])
ylabel('Y_{EtOH} [g/g]');
legend 'no adapted' 'adapted';
C = orderedcolors("reef");
colororder(C(2:3,:));
%corrected v
names={'24h';'48h';'72h'};
hold on
y2= [0.74 0.74; 0.44 0.57; 0.37 0.44];
plot(y2, '*', 'HandleVisibility', 'off');
set(gca,'xtick',1:3,'xticklabel',names);

4 commentaires

Ludovica Varriale
Ludovica Varriale le 20 Oct 2023
thank you!
Is there a way to have the marker on the corresponding bar? they are between the bars
y= [0.59 0.78; 0.61 0.84; 0.74 0.98];
b = bar(y);
ylim([0 1])
ylabel('Y_{EtOH} [g/g]');
legend({'no adapted' 'adapted'}, 'Location', 'northwest');
C = orderedcolors("reef");
colororder(C(2:3,:));
names={'24h';'48h';'72h'};
%Get the points corresponding to center of bars
x = [b(1).XEndPoints;b(2).XEndPoints]';
hold on
y2= [0.74 0.74; 0.44 0.57; 0.37 0.44];
scatter(x, y2, 'r*', 'HandleVisibility', 'off');
set(gca,'xtick',1:3,'xticklabel',names);
Ludovica Varriale
Ludovica Varriale le 20 Oct 2023
Dear Dyuman, this was exactly what I was searching for!!
Thank you for helping !
Dyuman Joshi
Dyuman Joshi le 20 Oct 2023
You are welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

x=["24h","48h","72h"];
y= [0.59 0.78; 0.61 0.84; 0.74 0.98];
yyaxis left
bh = bar(x,y);
ylim([0 1])
ylabel('Y_{EtOH} [g/g]');
C = orderedcolors("reef");
colororder(C(2:3,:));
hold on
yyaxis right
names={'24h';'48h';'72h'};
x=["24h","48h","72h"];
Cx = categorical(x);
y2= [0.74 0.74; 0.44 0.57; 0.37 0.44];
plot(Cx, y2, '*');
set(gca,'xtick',Cx,'xticklabel',names);
legend(bh, {'no adapted' 'adapted'}, 'location', 'north');

2 commentaires

Ludovica Varriale
Ludovica Varriale le 20 Oct 2023
thank you!
Is there a way to have the marker on the corresponding bar? they are between the bars
Walter Roberson
Walter Roberson le 20 Oct 2023
Not when you are using categorical x for the bar() call: when you do that then plot() coordinates can only be category names without any offset.
You would need to switch to using numeric x for the bar() call, knowing that your settting xticks is going to give the appropriate label. After that it would be a matter of figuring out where the bar positions are; that could either be estimated or could be done by examining the graphics objects created by the bar() call.

Connectez-vous pour commenter.

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by