필터 지우기
필터 지우기

Subplot x-axis

조회 수: 6 (최근 30일)
Moustafa Abedel Fattah
Moustafa Abedel Fattah 2024년 4월 11일
댓글: Voss 2024년 4월 15일
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일
편집: Adam Danz 2024년 4월 12일
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일
😳 sorry about that!
@Moustafa Abedel Fattah please note my mistake that Voss pointed out
Voss
Voss 2024년 4월 15일
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일
편집: Voss 2024년 4월 11일
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일
Good and accepted answer
Voss
Voss 2024년 4월 14일
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