MATLAB Answers

Reversing y axis direction causes xticks to invert and overlap with labels

66 ビュー (過去 30 日間)
Kelly Hokanson
Kelly Hokanson 2018 年 11 月 21 日
回答済み: Robert Weigel 2021 年 5 月 13 日
I just switched from 2015a to 2018b and encounted a problem with the xticks and corresponding labels whenever I invert the y-axis. The Xtick direction seems reversed from the set value (in or out) and they overlap with the labels. I have no clue how to fix this.
Thanks!
subplot(1,3,1)
plot(rand(10,1),rand(10,1).*(1E-8))
subplot(1,3,2)
plot(rand(10,1),rand(10,1).*(1E-8))
set(gca,'Ydir','reverse')
subplot(1,3,3)
plot(rand(10,1),rand(10,1).*(1E-8))
set(gca,'Ydir','reverse')
set(gca,'TickDir','out')
  4 件のコメント
Kelly Hokanson
Kelly Hokanson 2018 年 11 月 21 日
From the support team:
"I have been investigating the issue for a while now and unfortunately have not been able to come up with a workaround yet. I believe the issue is caused due to the presence of the exponent in the yaxis. The code snippet you provided on the MATLAB Answers page works as expected when (1E-8) exponent is not present.
If the exponent value of the y data is known previously, perhaps we could plot the y data without the exponent and specify the exponent in the ylabel. The following example shows how we may be able to achieve this.
>> f=figure;
>> x=1:10; y=rand(10, 1)*(1E-8);
>> Exp = max(floor(log10(y)));
>> plot(x, y*(10^-Exp));
>> ylabel(strcat(' x 10e', num2str(Exp)))
>> set(gca, 'Ydir', 'reverse')

サインインしてコメントする。

採用された回答

Samuel Coakley
Samuel Coakley 2019 年 5 月 31 日
編集済み: Samuel Coakley 2019 年 5 月 31 日
So I ran into the same problem and my solution is below.
Say you are plotting some data in A. Instead of plotting just A, plot -A and change the y tick labels. This means you don't have to reverse the axis so the x tick labels are still in the right place.
plot(1:length(A), -A)
yt = get(gca,'YTickLabel');
for ii=1:length(yt)
ytt{ii} = yt{ii}(2:end); % This trims the minus sign from the negative numbers
end
set(gca, 'YTickLabel', ytt)
  1 件のコメント
Kelly Hokanson
Kelly Hokanson 2019 年 6 月 4 日
This has been addressed in the newest release

サインインしてコメントする。

その他の回答 (2 件)

Stefanie Schwarz
Stefanie Schwarz 2021 年 3 月 22 日
編集済み: Stefanie Schwarz 2021 年 3 月 22 日
This is a bug in R2018b that was fixed in R2019a. See the following Bug Report:
A workaround is to set the Y-axis label to text and insert spaces.
Example:
>> set(gca, 'YTickLabel', {'0 ', '1 ', '2 ', '3 ', '4 '})

Robert Weigel
Robert Weigel 2021 年 5 月 13 日
Adding an empty subscript worked for the case when the x-tick labels have exponents.
xtl = get(gca,'XTickLabel'); % {'10^{1}', '10^{2}'}
for i = 1:length(xtl)
xtl{i} = [xtl{i},'_{ }']; % Append empty subscript
end
set(gca,'XTickLabel',xtl);

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by