MATLAB Answers

0

X and Y axis labels overwritten and illiegible

Asked by John Micco on 25 Jun 2018
Latest activity Commented on by Ameer Hamza
on 26 Jun 2018
I wrote the following code to put both the X and Y axis on log scale and do a scatter plot:
% Create figure
fig = figure;
title(['Test target transitions for ', date]);
set(fig, 'PaperOrientation', 'landscape')
xlabel('Number of Samples')
ylabel('Ratio of transitions / Number of Samples')
% Create axes
axes1 = axes('Parent',fig);
xtickformat('%10.4f');
ytickformat('%10.4f');
% Set the remaining axes properties
set(axes1,...
'XMinorTick','off',...
'XScale','log',...
'YMinorTick','off',...
'YScale','log');
% Create scatter
sflaky= scatter(X1,Y1);
hold(axes1,'on');
% Create scatter
snotflaky = scatter(X1,Y2);
p95Line = plot(points95(:,2), points95(:,1));
set(p95Line , 'LineWidth', 4);
p99Line = plot(points99(:,2), points99(:,1));
set(p99Line, 'LineWidth', 4);
p999Line = plot(points999(:,2), points999(:,1));
set(p999Line, 'LineWidth', 4);
legend([sflaky, snotflaky, p95Line, p99Line, p999Line], ...
{'flaky', 'not flaky', '95% pFlaky', '99% pFlaky', '99.9% pFlaky'});
When I run the code, I get garbled X and Y tick labels and I am not sure why. Reproduced both online and in my local version of MATLAB.
See the attached PDF file for an example of what it looks like.

  0 Comments

Sign in to comment.

Products


Release

R2018a

2 Answers

Answer by Ameer Hamza
on 25 Jun 2018

I don't have your data, therefore, can't test the complete code but I guess the problem is happening because your code is effectively creating 2 axes objects, which overlaps and creating illegible text. Try changing the order of lines at the beginning of your code as follow
fig = figure;
% Create axes
axes1 = axes('Parent',fig);
title(['Test target transitions for ', date]);
set(fig, 'PaperOrientation', 'landscape')
xlabel('Number of Samples')
ylabel('Ratio of transitions / Number of Samples')
xtickformat('%10.4f'); ytickformat('%10.4f');
% Set the remaining axes properties
set(axes1,...
'XMinorTick','off',...
'XScale','log',...
'YMinorTick','off',...
'YScale','log');

  0 Comments

Sign in to comment.


Answer by John Micco on 25 Jun 2018

Thanks so much for your timely answer! It was very helpful and the multiple axes problem was not something that occurred to me!
I ended up with:
% Create figure
fig = figure;
axes1 = gca;
hold(axes1,'on');
title(['Test target transitions for ', date]);
set(fig, 'PaperOrientation', 'landscape')
xlabel('Number of Samples')
ylabel('Ratio of transitions / Number of Samples')
xtickformat('%10.4f');
ytickformat('%10.4f');
% Set the remaining axes properties
set(axes1,...
'XScale','log',...
'YScale','log');
% Create scatter
sflaky= scatter(X1,Y1);
% Create scatter
snotflaky = scatter(X1,Y2);
p95Line = plot(points95(:,2), points95(:,1));
set(p95Line , 'LineWidth', 4);
p99Line = plot(points99(:,2), points99(:,1));
set(p99Line, 'LineWidth', 4);
p999Line = plot(points999(:,2), points999(:,1));
set(p999Line, 'LineWidth', 4);
legend([sflaky, snotflaky, p95Line, p99Line, p999Line], ...
{'flaky', 'not flaky', '95% pFlaky', '99% pFlaky', '99.9% pFlaky'});
The only remaining question is how do I get the Axes labels to come out as floating point and not exponentials? I have seen some answers but all involve re-parsing the labels put on by "Log" view.
See the attached PDF.

  3 Comments

Although you can use axes1.XAxis.TickLabelFormat property to specify the label format but it does not seem to effect in the log-scale view. You might need to do it manually like this
axes1.XTickLabel = {'1', '10', '100', '1000', '10000'};
axes1.YTickLabel = {'0.0001', '0.001', '0.01', '0.1', '1'};
Another option is
axes1.XTickLabel = arrayfun(@(x) {num2str(x)}, logspace(0, 4, 5));
axes1.YTickLabel = arrayfun(@(x) {num2str(x)}, logspace(-4, 0, 5));
This answer gives up on the auto scaling and would not work if I use a set with different ranges. It seems like I ought to be able to control the generation of the XTick labels using the label format - could that be a bug?
I am not sure whether it is a bug. It might be an intended behavior because it is more convenient to display and read values in the exponential form in log scale. Although it would be nice if MATLAB still provide control over label format even in log scale.

Sign in to comment.