Subplot x-axis

40 次查看(过去 30 天)
Moustafa Abedel Fattah
Moustafa Abedel Fattah 2024-4-11,18:39
评论: Voss 2024-4-15,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]);

采纳的回答

Adam Danz
Adam Danz 2024-4-12,16:41
编辑:Adam Danz 2024-4-12,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 个评论
Adam Danz
Adam Danz 2024-4-15,14:47
😳 sorry about that!
@Moustafa Abedel Fattah please note my mistake that Voss pointed out
Voss
Voss 2024-4-15,15:05
No problem, I just wanted it to be clear to OP that the changes you listed were relative to my answer.

请先登录,再进行评论。

更多回答(1 个)

Voss
Voss 2024-4-11,19:12
编辑:Voss 2024-4-11,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 个评论
Moustafa Abedel Fattah
Moustafa Abedel Fattah 2024-4-14,11:46
Good and accepted answer
Voss
Voss 2024-4-14,13:54
Thank you! You accepted the other answer, which was based on this answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by