How to move the plot upward while still displaying the origin as (0,0)
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello:
I am trying to plot something like this, I've searched around online:
tried: <xlim/ylim> options, appraently, it doesn't provide this kind of open Y+ and X+.
tried
ax = agc
ax.XAxisLocation/ax.YAxislocation as mentioned on the forum, still didn't live up to the expectaton.

, Can anyone provide some input on this?
Thank you in advance
0 comentarios
Respuestas (1)
BhaTTa
el 29 de Mayo de 2025
Hey @Mamatweli T, I understand the kind of plot presentation you're aiming for. It's a common request to have your data nicely framed within the positive quadrant and stacked one above the other , with the origin (0,0) clearly visible
Please refer to below code and take it as reference and modify it accordingly:
% --- 1. Define Your Data (Example for Three Plots) ---
x_common = 0:0.1:2*pi; % Common X-axis range for consistency
% Data for the top (sine graph)
y_sine = sin(x_common) + 1; % Shifted so min Y is 0. If it can go negative,
% adjust YLim min below for clarity or handle negative data range.
% Data for the middle (dotted sine graph)
y_dotted_sine = sin(x_common * 2) + 0.5;
% Data for the bottom (a line)
y_line = x_common * 0.5;
% --- 2. Create the "Original" Plot (Default MATLAB Behavior) ---
% This shows what your plots might look like without explicit limits
figure('Name', 'Original Plots (Default Limits)');
% Subplot 1: Sine Graph
subplot(3, 1, 1);
plot(x_common, y_sine, 'b-', 'DisplayName', 'Sine Graph');
title('Top: Sine Graph (Default)');
xlabel('X'); ylabel('Y'); grid on;
ax1_orig = gca; % Get handle to the first subplot's axes
% Subplot 2: Dotted Sine Graph
subplot(3, 1, 2);
plot(x_common, y_dotted_sine, 'r:', 'LineWidth', 1.5, 'DisplayName', 'Dotted Sine Graph');
title('Middle: Dotted Sine Graph (Default)');
xlabel('X'); ylabel('Y'); grid on;
ax2_orig = gca; % Get handle to the second subplot's axes
% Subplot 3: Line Graph
subplot(3, 1, 3);
plot(x_common, y_line, 'g-', 'DisplayName', 'Line Graph');
title('Bottom: Line Graph (Default)');
xlabel('X'); ylabel('Y'); grid on;
ax3_orig = gca; % Get handle to the third subplot's axes
% Display the original limits for comparison
disp('--- Original Plot Limits (Default MATLAB) ---');
disp(['Top Plot XLim: ' mat2str(ax1_orig.XLim) ' YLim: ' mat2str(ax1_orig.YLim)]);
disp(['Middle Plot XLim: ' mat2str(ax2_orig.XLim) ' YLim: ' mat2str(ax2_orig.YLim)]);
disp(['Bottom Plot XLim: ' mat2str(ax3_orig.XLim) ' YLim: ' mat2str(ax3_orig.YLim)]);
% --- 3. Create the "Adjusted" Plot (Showing Origin & More Space) ---
figure('Name', 'Adjusted Plots (Origin Visible, More Space)');
% A margin factor to extend limits beyond max data (e.g., 20% extra space)
margin_factor_x = 1.2;
margin_factor_y = 1.2;
% --- Subplot 1: Sine Graph ---
subplot(3, 1, 1);
plot(x_common, y_sine, 'b-', 'DisplayName', 'Sine Graph');
title('Top: Sine Graph (Adjusted)');
xlabel('X'); ylabel('Y'); grid on;
ax1_adj = gca; % Get current axes handle for this subplot
% Adjust limits for this subplot
ax1_adj.XLim = [0 max(x_common) * margin_factor_x]; % X-axis from 0
ax1_adj.YLim = [min(0, min(y_sine)) max(y_sine) * margin_factor_y]; % Y-axis from 0 (or lower if data is negative)
% --- Subplot 2: Dotted Sine Graph ---
subplot(3, 1, 2);
plot(x_common, y_dotted_sine, 'r:', 'LineWidth', 1.5, 'DisplayName', 'Dotted Sine Graph');
title('Middle: Dotted Sine Graph (Adjusted)');
xlabel('X'); ylabel('Y'); grid on;
ax2_adj = gca; % Get current axes handle for this subplot
% Adjust limits for this subplot
ax2_adj.XLim = [0 max(x_common) * margin_factor_x];
ax2_adj.YLim = [min(0, min(y_dotted_sine)) max(y_dotted_sine) * margin_factor_y];
% --- Subplot 3: Line Graph ---
subplot(3, 1, 3);
plot(x_common, y_line, 'g-', 'DisplayName', 'Line Graph');
title('Bottom: Line Graph (Adjusted)');
xlabel('X'); ylabel('Y'); grid on;
ax3_adj = gca; % Get current axes handle for this subplot
% Adjust limits for this subplot
ax3_adj.XLim = [0 max(x_common) * margin_factor_x];
ax3_adj.YLim = [min(0, min(y_line)) max(y_line) * margin_factor_y];
% Display the new limits for comparison
disp(' ');
disp('--- Adjusted Plot Limits ---');
disp(['Top Plot XLim: ' mat2str(ax1_adj.XLim) ' YLim: ' mat2str(ax1_adj.YLim)]);
disp(['Middle Plot XLim: ' mat2str(ax2_adj.XLim) ' YLim: ' mat2str(ax2_adj.YLim)]);
disp(['Bottom Plot XLim: ' mat2str(ax3_adj.XLim) ' YLim: ' mat2str(ax3_adj.YLim)]);
0 comentarios
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!