Clear Filters
Clear Filters

Bar Plot Legend and Axes

8 views (last 30 days)
Fawad Farooq Ashraf
Fawad Farooq Ashraf on 7 Mar 2022
I have some data which I plotted as,
What I want to do would look something similar to,
I opened another figure and copied the subplots manually, I don't know how to code this to automatically work and look cleaner.
Moreover, the legend entries don't seem to match colors. I think it's not correct.
Is it possible that I remove the YTickLabels from the subplots axes and instead write down the values on the bar chart.
Any help would be highly appreciated.
There's a lot of difference between values. If I plot them all on one plot, then some data might look too small.
My code is given below
clear;close all;clc
set(0,'DefaultLineLineWidth',2);
successful_steps = [17;48;21;18];
rejected_steps = [3;17;6;4];
func_evaluations = [145;182;165;148];
execution_time = [110;80;66;61];
figure(1);
subplot(141);hold on;grid off;box off;
x = gca;
b1 = bar(successful_steps,'FaceColor','Flat');
b1.CData(1,:) = [0 0.8 0.8];
b1.CData(2,:) = [0.4940 0.1840 0.5560];
b1.CData(3,:) = [0.6350 0.0780 0.1840];
b1.CData(4,:) = [0 0.4470 0.7410];
xlabel({'Successful';'Steps'},'FontSize',12);
x.XTickLabels = {''};
subplot(142);hold on;grid off;box off;
x = gca;
b1 = bar(rejected_steps,'FaceColor','Flat');
b1.CData(1,:) = [0 0.8 0.8];
b1.CData(2,:) = [0.4940 0.1840 0.5560];
b1.CData(3,:) = [0.6350 0.0780 0.1840];
b1.CData(4,:) = [0 0.4470 0.7410];
xlabel({'Rejected';'Steps'},'FontSize',12);
x.XTickLabels = {''};
subplot(143);hold on;grid off;box off;
x = gca;
b1 = bar(func_evaluations,'FaceColor','Flat');
b1.CData(1,:) = [0 0.8 0.8];
b1.CData(2,:) = [0.4940 0.1840 0.5560];
b1.CData(3,:) = [0.6350 0.0780 0.1840];
b1.CData(4,:) = [0 0.4470 0.7410];
xlabel({'Function';'Evaluations'},'FontSize',12);
x.XTickLabels = {''};
subplot(144);hold on;grid off;box off;
x = gca;
b1 = bar(execution_time,'FaceColor','Flat');
b1.CData(1,:) = [0 0.8 0.8];
b1.CData(2,:) = [0.4940 0.1840 0.5560];
b1.CData(3,:) = [0.6350 0.0780 0.1840];
b1.CData(4,:) = [0 0.4470 0.7410];
xlabel({'Execution';'Time'},'FontSize',12);
x.XTickLabels = {''};
sgtitle('Performance Comparison of xyz Methods','FontSize',16,'FontWeight','Bold');
figure(2);hold on;grid off;box on
ax = gca;
h1 = plot(0,[0 0.8 0.8]);
h2 = plot(0,[0.4940 0.1840 0.5560]);
h3 = plot(0,[0.6350 0.0780 0.1840]);
h4 = plot(0,[0 0.4470 0.7410]);
legend('M1','M2','M3','M4','Orientation','Horizontal','Box','Off');
ax.XTickLabels = {''};
ax.YTickLabels = {''};
title('Performance Comparison of xyz Methods','FontSize',16,'FontWeight','Bold');
xlabel({'';''},'FontSize',12);

Answers (1)

Constantino Carlos Reyes-Aldasoro
Hi
First, to plot the subplots together, you need to grab the handles of each axes, i.e.
figure
h1=subplot(141);
h2=subplot(142);
h3=subplot(143);
h4=subplot(144);
h4=subplot(144);
The handles contain the position as well as many many more things:
h1.Position
ans = 1×4
0.1300 0.1100 0.1566 0.8150
which comes in starting point horizontal/vertical size horizontal/vertical.
Once you have the basic position, you can modify, if you want them to be exactly next to each other, you can just make them of a certain width (say 0.2) and then separate them by the same:
h1.Position =[0.1 0.1 0.2 0.8];
h2.Position =[0.3 0.1 0.2 0.8];
h3.Position =[0.5 0.1 0.2 0.8];
h4.Position =[0.7 0.1 0.2 0.8];
and presto, just next to each other. Let me think about the legend.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!