MATLAB Answers

How to maintain uniformity in the xtick and ytick labels? Also, how to adjust the legend position in a non-interactive setup.

1 view (last 30 days)
Jay Vaidya
Jay Vaidya on 4 Oct 2020
Commented: Adam Danz on 4 Oct 2020
I have the below plot and I want to do the following changes.
1) If you see the horizontal spacing between tick and the plot is not equal to that of vertical spacing of x ticks and the plot. How to change that as I couldn't find a way to do so.
2) How to drag the legend position a bit higher in a non-interactive setup so that it doesn't overlap the plot data points, with the axis limits being the same.
It would be great if someone could suggest as I am trying to find the solution but I am unable to.
Code:
clc
clear all
close all
%Prompt user for filename
[fname, pname] = uigetfile('*.csv');
%Create fully-formed filename as a string
filename = fullfile(pname, fname);
%Check that file exists
assert(exist(filename,'file')==2, '%s does not exist.', filename);
%Read in the data, skipping the first row
data = csvread(filename,1,0);
%%
figure('Position', [1 1 4000 700])
plot(data(:,1),data(:,2),'b','LineWidth',1);
hold on
plot(data(:,1),data(:,3),'color', [0,0.5,0],'LineWidth',1);
axis([7.5e-3 11.1e-3 1.8 2.49])
xticks([7.8e-3 8.4e-3 9e-3 9.6e-3 10.2e-3 10.8e-3])
xticklabels({'7800','8400','9000','9600','10200','10800'})
yticks([2 2.2 2.4])
yticklabels({'2.0','2.2','2.4'})
legend('Oscillator A', 'Oscillator A', 'FontSize',48,'TextColor','black');
legend('boxoff')
legend('Orientation','horizontal')
legend('Location','north')
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'fontsize',48,'FontWeight','bold')
  3 Comments
Adam Danz
Adam Danz on 4 Oct 2020
"the horizontal spacing between tick and the plot is not equal to that of vertical spacing of x ticks and the plot."
Not sure what this means. Are you referring to the the tick intervals in data units or do you just want square grids (grid on)?
"How to drag the legend position a bit higher in a non-interactive setup so that it doesn't overlap the plot data points, with the axis limits being the same. "
try
legend(___, 'Location','NorthOutside')
or change the position property
lh = legend(___);
lh.Position(2) = lh.Position(2) * 1.05; % move 5% upward

Sign in to comment.

Accepted Answer

Alan Stevens
Alan Stevens on 4 Oct 2020
I'm not sure what you mean by the y-tick spacing being different from that of the x-ticks. I've let the fonts retain their default size - you can alter them to your hearts content. You have labelled both curves as oscillator A. Did you mean to do that?
Anyway, see if the following helps:
%Read in the data, skipping the first row
data = csvread(filename,1,0);
ix1=find(data(:,1)<7.5E-3,1,'last'); % find position of values between
ix2 = find(data(:,1)<11.1E-3,1,'last'); % 0.00753 and 0.011
x = data(ix1:ix2,1); % select ony those data points
y1 = data(ix1:ix2,2);
y2 = data(ix1:ix2,3);
%%
plot(x,y1,'b','LineWidth',1), grid
hold on
plot(x,y2,'r','LineWidth',1);
xticks([7.8e-3 8.4e-3 9e-3 9.6e-3 10.2e-3 10.8e-3])
xticklabels({'7800','8400','9000','9600','10200','10800'})
yticks([2 4 6 8])
h = legend('Oscillator A', 'Oscillator A', 'FontSize',12,'TextColor','black');
legend('boxoff')
legend('Orientation','horizontal')
h.Position = [0.2429 0.92 0.4750 0.0595];

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!