Clear Filters
Clear Filters

Subplot x-axis

41 views (last 30 days)
Moustafa Abedel Fattah
Moustafa Abedel Fattah on 11 Apr 2024 at 18:39
Commented: Voss on 15 Apr 2024 at 15:05
I need the two x-axis of subplots to be equal for the following code and use a loop for describe pattern data (see the annexed file)
Thanks in advance
%=================================================%
clc;
close all;
clear;
% Define the data
pattern = [
2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46;
2.68, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.68;
NaN, 2.68, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.68, NaN;
NaN, NaN, 2.68, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.68, NaN, NaN;
NaN, NaN, NaN, 2.68, 2.4, 2.4, 2.4, 2.4, 2.4, 2.68, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, 2.68, 2.5, 2.5, 2.5, 2.68, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, 2.68, 2.57, 2.68, NaN, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, NaN, 2.68, NaN, NaN, NaN, NaN, NaN, NaN;
];
% Define the data for (x, g)
x = [-6:6];
g = [-0.002 -0.004 -0.005 -0.008 -0.008 -0.014 -0.014 -0.014 -0.008 -0.008 -0.005 -0.004 -0.002];
% Define positions for text
[row, col] = find(~isnan(pattern));
% Define cell size
cellSize = 1;
% Define colors for each unique value
unique_values = unique(pattern(~isnan(pattern)));
colors = parula(length(unique_values)); % Change 'parula' to any colormap you prefer
% Create a new figure
figure;
% Plot the existing pattern
subplot(2, 1, 1); % Adjust subplot dimensions as needed
% Plot the square cells with distinct colors for each value
for i = 1:numel(row)
value = pattern(row(i), col(i));
color_index = find(unique_values == value);
rectangle('Position', [col(i)-0.5, row(i)-0.5, cellSize, cellSize], 'EdgeColor', 'k', 'FaceColor', colors(color_index,:));
text(col(i), row(i), num2str(value), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'FontSize', 10);
end
grid on
% Set axis limits
xlim([min(x), max(x)]);
ylim([0.5, size(pattern, 1) + 0.5]);
axis equal;
set(gca, 'YDir', 'reverse');
title('(x, g) Pattern');
% Plot (x, g)
subplot(2, 1, 2); % Adjust subplot dimensions as needed
plot(x, g, '-o');
grid on;
xlabel('x');
ylabel('g');
title('(x, g) Plot');
xlim([min(x), max(x)]); % Set the same x-axis limits as the pattern plot
% Adjust the position of subplots to make them visually aligned
set(gca, 'Position', get(gca, 'Position') + [0 0.05 0 0]);

Accepted Answer

Adam Danz
Adam Danz on 12 Apr 2024 at 16:41
Edited: Adam Danz on 12 Apr 2024 at 16:42
Use linkaxes to link the two x axis limits. When the limits of one x-axis changes, the other will adjust.
I made 6 changes to your code
  1. store the axes handles in the output of subplot() (x2)
  2. remove setting xlim on both axes (x2)
  3. Add linkaxes() toward the end
  4. Set xlim to either axes after linking axes
% Define the data
patt = [
2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46;
2.68, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.68;
NaN, 2.68, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.68, NaN;
NaN, NaN, 2.68, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.68, NaN, NaN;
NaN, NaN, NaN, 2.68, 2.4, 2.4, 2.4, 2.4, 2.4, 2.68, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, 2.68, 2.5, 2.5, 2.5, 2.68, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, 2.68, 2.57, 2.68, NaN, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, NaN, 2.68, NaN, NaN, NaN, NaN, NaN, NaN;
];
% Define the data for (x, g)
x = -6:6;
g = [-0.002 -0.004 -0.005 -0.008 -0.008 -0.014 -0.014 -0.014 -0.008 -0.008 -0.005 -0.004 -0.002];
% Define positions for text
[row, col] = find(~isnan(patt));
% Define cell size
cellSize = 1;
% Define colors for each unique value
unique_values = unique(patt(~isnan(patt)));
colors = parula(length(unique_values)); % Change 'parula' to any colormap you prefer
% Create a new figure
figure;
% Plot the existing pattern
ax1 = subplot(2, 1, 1); %<----------------- store handle
% Plot the square cells with distinct colors for each value
for i = 1:numel(row)
value = patt(row(i), col(i));
color_index = find(unique_values == value);
rectangle('Position', [col(i)-7.5, row(i)-0.5, cellSize, cellSize], 'EdgeColor', 'k', 'FaceColor', colors(color_index,:));
text(col(i)-7, row(i), num2str(value), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'FontSize', 10);
end
grid on
box on
% Set axis limits
% xlim([min(x)-0.5, max(x)+0.5]); <--------------remove
ylim([0.5, size(patt, 1) + 0.5]);
% axis equal;
set(gca, 'YDir', 'reverse');
title('(x, g) Pattern');
% Plot (x, g)
ax2 = subplot(2, 1, 2); % <-----------------store handle
plot(x, g, '-o');
grid on;
xlabel('x');
ylabel('g');
title('(x, g) Plot');
% xlim([min(x)-0.5, max(x)+0.5]); % <--------------- remove
linkaxes([ax1,ax2],'x') % <--------------- add
axis(ax2,'padded') % <-------------------- or set xlim()
  3 Comments
Adam Danz
Adam Danz on 15 Apr 2024 at 14:47
😳 sorry about that!
@Moustafa Abedel Fattah please note my mistake that Voss pointed out
Voss
Voss on 15 Apr 2024 at 15:05
No problem, I just wanted it to be clear to OP that the changes you listed were relative to my answer.

Sign in to comment.

More Answers (1)

Voss
Voss on 11 Apr 2024 at 19:12
Edited: Voss on 11 Apr 2024 at 19:12
Remove axis equal. Shift the rectangles and texts to the left by 7. Increase the width of the x-limits by 1 (0.5 on each side), to account for the width of a rectangle.
% Define the data
patt = [
2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46;
2.68, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.29, 2.68;
NaN, 2.68, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.68, NaN;
NaN, NaN, 2.68, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.68, NaN, NaN;
NaN, NaN, NaN, 2.68, 2.4, 2.4, 2.4, 2.4, 2.4, 2.68, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, 2.68, 2.5, 2.5, 2.5, 2.68, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, 2.68, 2.57, 2.68, NaN, NaN, NaN, NaN, NaN;
NaN, NaN, NaN, NaN, NaN, NaN, 2.68, NaN, NaN, NaN, NaN, NaN, NaN;
];
% Define the data for (x, g)
x = -6:6;
g = [-0.002 -0.004 -0.005 -0.008 -0.008 -0.014 -0.014 -0.014 -0.008 -0.008 -0.005 -0.004 -0.002];
% Define positions for text
[row, col] = find(~isnan(patt));
% Define cell size
cellSize = 1;
% Define colors for each unique value
unique_values = unique(patt(~isnan(patt)));
colors = parula(length(unique_values)); % Change 'parula' to any colormap you prefer
% Create a new figure
figure;
% Plot the existing pattern
subplot(2, 1, 1); % Adjust subplot dimensions as needed
% Plot the square cells with distinct colors for each value
for i = 1:numel(row)
value = patt(row(i), col(i));
color_index = find(unique_values == value);
rectangle('Position', [col(i)-7.5, row(i)-0.5, cellSize, cellSize], 'EdgeColor', 'k', 'FaceColor', colors(color_index,:));
text(col(i)-7, row(i), num2str(value), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'FontSize', 10);
end
grid on
box on
% Set axis limits
xlim([min(x)-0.5, max(x)+0.5]);
ylim([0.5, size(patt, 1) + 0.5]);
% axis equal;
set(gca, 'YDir', 'reverse');
title('(x, g) Pattern');
% Plot (x, g)
subplot(2, 1, 2); % Adjust subplot dimensions as needed
plot(x, g, '-o');
grid on;
xlabel('x');
ylabel('g');
title('(x, g) Plot');
xlim([min(x)-0.5, max(x)+0.5]); % Set the same x-axis limits as the pattern plot
  2 Comments
Moustafa Abedel Fattah
Moustafa Abedel Fattah on 14 Apr 2024 at 11:46
Good and accepted answer
Voss
Voss on 14 Apr 2024 at 13:54
Thank you! You accepted the other answer, which was based on this answer.

Sign in to comment.

Categories

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

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!