Filter löschen
Filter löschen

Partitioning and putting titles in a subplot

1 Ansicht (letzte 30 Tage)
Reji G
Reji G am 3 Jun. 2023
Kommentiert: Reji G am 4 Jun. 2023
clc; close all; clear all;
x=[0 1 2 3 6 4 5 8 5];y=[9 8 5 6 3 2 1 5 2];
subplot(4,4,1); plot(x,y);subplot(4,4,2); plot(x,y);subplot(4,4,3); plot(x,y);
subplot(4,4,4); plot(x,y);subplot(4,4,5); plot(x,y);subplot(4,4,6); plot(x,y);
subplot(4,4,7); plot(x,y);subplot(4,4,8); plot(x,y);subplot(4,4,9); plot(x,y);
subplot(4,4,10); plot(x,y);subplot(4,4,11); plot(x,y);subplot(4,4,12); plot(x,y);
subplot(4,4,13); plot(x,y);subplot(4,4,14); plot(x,y);subplot(4,4,15); plot(x,y);subplot(4,4,16); plot(x,y);
How to put partitioning as shown in the image and write title for each category ?

Antworten (3)

Tushar
Tushar am 3 Jun. 2023
Hi Reji,
Please try like this:
clc; close all; clear all;
x = [0 1 2 3 6 4 5 8 5];
y = [9 8 5 6 3 2 1 5 2];
figure; % Create a new figure
for i = 1:16
subplot(4, 4, i); % Create each subplot
plot(x, y);
% Add title to each subplot
title(sprintf('Subplot %d', i));
% Adjust subplot spacing
if mod(i, 4) ~= 1
ax = gca;
ax.YAxis.Visible = 'off';
end
if i <= 12
ax.XAxis.Visible = 'off';
end
end
In this modified code, I've added a loop to create each subplot using subplot(4, 4, i), where i corresponds to the index of the subplot. Then, I plot the data in each subplot using plot(x, y).
To add titles to each subplot, I've used the title function with a formatted string sprintf('Subplot %d', i), where i represents the index of the subplot.
I've also adjusted the subplot spacing by hiding the y-axis labels for subplots that are not on the leftmost column (if mod(i, 4) ~= 1) and hiding the x-axis labels for subplots below the last row (if i <= 12).
  1 Kommentar
Reji G
Reji G am 4 Jun. 2023
Thank you. But I need partitioning lines to be visible. (Please see the red dashed line in my drawing)

Melden Sie sich an, um zu kommentieren.


VBBV
VBBV am 4 Jun. 2023
Bearbeitet: VBBV am 4 Jun. 2023
If you want partioning using subplot titles then below is one method
clc; close all; clear all;
x = [0 1 2 3 6 4 5 8 5];
y = [9 8 5 6 3 2 1 5 2];
figure; % Create a new figure
k = 1;
for I = 1:16
subplot(4, 4, I); % Create each subplot
plot(x, y);
if I <= (4)
% Add title to each subplot
title(sprintf('Cat %d', k));
k = k+1;
elseif I > 4 & I <=8
title('')
elseif I > 8 & I <=12
title(sprintf('Cat %d', k));
k = k+1;
else
title('')
end
end
otherwise, you can also add space between the subplots using Position property of subplot
  1 Kommentar
Reji G
Reji G am 4 Jun. 2023
Thank you. But I need partitioning lines to be visible. (Please see the red dashed line in my drawing)

Melden Sie sich an, um zu kommentieren.


Tushar
Tushar am 4 Jun. 2023
Hi Reji,
Please do it like the following:
I know the partition lines are encroaching over the subplots : ), but I thought you yourself would be better able to adjust them according to your requirements.
clc; close all; clear all;
x = [0 1 2 3 6 4 5 8 5];
y = [9 8 5 6 3 2 1 5 2];
figure; % Create a new figure
for i = 1:16
subplot(4, 4, i); % Create each subplot
plot(x, y);
% Add title to each subplot
title(sprintf('Cat %d', i)); % Change "Subplot" to "Cat"
% Adjust subplot spacing
if mod(i, 4) ~= 1
ax = gca;
ax.YAxis.Visible = 'off';
end
if i <= 12
ax.XAxis.Visible = 'off';
end
% Add partition lines
if i > 4
ax = gca;
hold on;
line([min(x)-0.5, min(x)-0.5], ylim, 'Color', 'r', 'LineStyle', '--', 'LineWidth', 1.5);
hold off;
end
if i <= 12 && mod(i, 4) ~= 0
ax = gca;
hold on;
line(xlim, [min(y)-2, min(y)-2], 'Color', 'r', 'LineStyle', '--', 'LineWidth', 1.5);
hold off;
end
end
  1 Kommentar
Reji G
Reji G am 4 Jun. 2023
Thank yoy for your time. I tried with the help of existing responses. But I couldn't adjust as like my hand sketch. That's why I posted it here. My requirement is well drawn in the sketch.
I need partition lines without break. Your red line is breaking at the end of a subplot.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by