Azzera filtri
Azzera filtri

Subplot x-axis

6 visualizzazioni (ultimi 30 giorni)
Moustafa Abedel Fattah
Moustafa Abedel Fattah il 11 Apr 2024
Commentato: Voss il 15 Apr 2024
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]);

Risposta accettata

Adam Danz
Adam Danz il 12 Apr 2024
Modificato: Adam Danz il 12 Apr 2024
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 Commenti
Adam Danz
Adam Danz il 15 Apr 2024
😳 sorry about that!
@Moustafa Abedel Fattah please note my mistake that Voss pointed out
Voss
Voss il 15 Apr 2024
No problem, I just wanted it to be clear to OP that the changes you listed were relative to my answer.

Accedi per commentare.

Più risposte (1)

Voss
Voss il 11 Apr 2024
Modificato: Voss il 11 Apr 2024
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 Commenti
Moustafa Abedel Fattah
Moustafa Abedel Fattah il 14 Apr 2024
Good and accepted answer
Voss
Voss il 14 Apr 2024
Thank you! You accepted the other answer, which was based on this answer.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by