How To Create a Space Between Edge of a Plot and The Y-axis LineTick?

35 views (last 30 days)
How Can I create a space between the edge of the right hand plot and the y-axis lineTick from the code below? That is, to seperate the plot edge from the right y-axis Tickline.
This code generates the figure in plot1. But the figure in plot2 is desired.
desired_azimuth = 272;
figure('position',[100 100 900 550])
hold on
yyaxis left
plot(range, pia_cumulative12191, 'LineWidth', 2.0, 'Color', 'k') % Set plot color to black
set(gca, 'FontName', 'Arial', 'FontWeight', 'bold', 'xlim', [30 60], 'ylim', [0 25], 'FontSize', 14, 'YColor', 'k', 'YMinorTick', 'on') % Set axis properties
xlabel('Range (km)', 'FontSize', 15)
ylabel('Cumulative PIA (dB/km)', 'FontSize', 15, 'Color', 'k') % Set label color to black
yyaxis right
plot(range, pia_ref_12191, 'LineWidth', 2.0, 'Color', 'b') % Set plot color to blue
set(gca, 'ylim', [-10 70], 'FontSize', 14, 'YColor', 'b', 'YMinorTick', 'on') % Set axis properties including minor ticks
ylabel('Reflectivity (dBZ)', 'FontSize', 15, 'Color', 'b') % Set label color to blue
line = xline([39.2 45.3], '--k', 'LineWidth', 2.0);
str = {'Region of', 'Melting Ice'};
text(40.5, 2.0, str, 'Color', 'black', 'FontSize', 15)
label1 = 'PIA';
label2 = 'Z_{Measured}';
legend(label1, label2, 'Interpreter', 'tex', 'Location', 'northwest', 'FontSize', 15, 'EdgeColor', 'k', 'FontWeight', 'bold'); % Set legend properties
title(['PIA and Reflectivity', ' | ', 'Azimuth: ', num2str(desired_azimuth), '^o', ' | ', 'Elevation: 0.7', '^o', ' | ', '12:19 UTC'], ...
'FontSize', 15, 'FontWeight', 'bold', 'color', 'k');
grid on
set(gca, 'LineWidth', 2, 'Box', 'on') % Set edge box of the entire plot to bold
hold off
  1 Comment
vidyesh
vidyesh on 10 Apr 2024
Adding the below line of code will not add a gap between the figure and ticks but it may help in addressing the issue:
set(gca,"TickDir",'out')

Sign in to comment.

Answers (2)

Rushikesh
Rushikesh on 16 Aug 2024
Edited: Rushikesh on 16 Aug 2024
Hello @Tunde Adubi,
I can see that you aim to create a plot where the edge of the plotted data is visually distinct from the y-axis tick lines. Specifically, you want to display two separate y-axes on the plot. The first y-axis should function solely as a ruler, showing only the axis line and tick marks without any data labels. The second y-axis should include the axis line, tick marks, and labels, positioned at a distance from the first axis to ensure clear separation between the two.
You can achieve this by creating two axes on same plot. You can use first axes for displaying only ruler and plotting data. On you second axis you can plot ticks, labels and to shift it to say right direction you can modify ‘Position’ property of second axes.
Sample Code is given below:
% Sample data
x = 1:10;
y1 = rand(1, 10); % Data for the first y-axis
y2 = rand(1, 10) * 100; % Data for the second y-axis
% Create a figure
figure;
% Plot the first y-axis
h1 = plot(x, y1, '-o', 'DisplayName', 'Data 1');
% Create a second axes, overlaying the first
ax1 = gca; % Get handle to the first axes
ax1.YTickLabel = {}; % Hide y-axis labels for the first axis
ax2 = axes('Position', ax1.Position, ... % Overlay the second axes
'Color', 'none', ... % Make background transparent
'YColor', 'r', ... % Set color for the second y-axis
'XColor', 'none', ... % Hide x-axis of the second axes
'Box', 'off');
% Shift the second y-axis to the right
ax2.Position(1) = ax2.Position(1) - 0.02; % Adjust this value to increase the gap
% keeping both same
ax1.YLim = [0, 1];
ax2.YLim = [0, 1];
% Ensure the first axes is on top
uistack(ax1, 'top');
% Add legend
legend([ax1.Children; ax2.Children], 'Location', 'best');
Above code is tested in MATLAB R2024a.
You can refer to MATLAB axes properties documentation attached below to understand different properties including ‘Position’ in more detail.
You can make these changes on right side of y axis using 'yyaxis' function.
Let me know if this helps.

Star Strider
Star Strider on 16 Aug 2024
No data,m so I can’t run your code.
That aside, try this —
x = 0:0.1:10;
y = randn(size(x));
figure
plot(x,y)
grid
xlabel('X')
ylabel('Y')
title('Unpadded X')
figure
plot(x,y)
grid
xlim('padded')
xlabel('X')
ylabel('Y')
title('Padded X')
If you want the plot padded in both directions, use ‘axis('padded')’
figure
plot(x,y)
grid
axis('padded')
xlabel('X')
ylabel('Y')
title('Padded X & Y')
.

Categories

Find more on Graphics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!