フィルターのクリア

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 Center および File Exchange2-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