37 views (last 30 days)
Show older comments
Arvind Ganesh on 2 Nov 2018
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/427639-flip-the-y-axis-ticks-without-flipping-the-plot
Edited: Pradyumna Byappanahalli Suresha on 15 Apr 2020
Accepted Answer: madhan ravi
I'm having a 2D scatter plot. The Y-axis ticks go like 0,5,10..35
I want the plot to remain as it is, but flip the ticks so that they go like 35,30...5,0.
Can this be done?
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Adam on 2 Nov 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/427639-flip-the-y-axis-ticks-without-flipping-the-plot#comment_631822
Edited: Adam on 2 Nov 2018
Surely if you flip only the y axis values then the plot will be wrong if it stays as is? If flipping only the y axis is what makes it correct then I would suggest you plot your data correctly in the first place so that it is consistent.
Flipping the y axis is just a matter of whether you have the origin at the top or the bottom, but the data will move with it, if you just change the YDir setting of the axes
Sign in to comment.
Sign in to answer this question.
Accepted Answer
madhan ravi on 2 Nov 2018
Open in MATLAB Online
yticklabels([1:10]) %an example
set ( gca, 'ydir', 'reverse' )
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Arvind Ganesh on 2 Nov 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/427639-flip-the-y-axis-ticks-without-flipping-the-plot#comment_631821
Thanks. Actually, I intended to ask another question, that is, how to flip the sign of the ticks (i.e. 0,5,10...35 to 0,-5,-10..-35), but mistakenly asked this. Your answer does answer the question so I'll accept this answer.
madhan ravi on 2 Nov 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/427639-flip-the-y-axis-ticks-without-flipping-the-plot#comment_631825
Anytime :)
Adam on 2 Nov 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/427639-flip-the-y-axis-ticks-without-flipping-the-plot#comment_631828
It doesn't answer the question you actually asked! It gives a good solution if you want to flip the axes and have the data consistently flip with it, as you would often want to happen. Wanting the axes to flip but the data to stay put is not generally desirable though anyway.
Sign in to comment.
More Answers (2)
Elias Gule on 2 Nov 2018
Open in MATLAB Online
I guess you want something like what the ff code produces.
x = 30:5:65;
y = 0:5:35;
scatter(x,y);
yTickLabels = arrayfun(@num2str,sort(y,'descend'),'uni',false);
ax = gca;
ax.YAxis.TickLabels = yTickLabels;
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Pradyumna Byappanahalli Suresha on 15 Apr 2020
Edited: Pradyumna Byappanahalli Suresha on 15 Apr 2020
Open in MATLAB Online
If you are plotting a matrix via imagesc or something similar, below method helps in inverting the tickAxes without flipping the data. This is an improved version of Elias Gule's answer.
% Generate a random 100X100 matrix and plot it via `imagesc`.
s = rand(100,100);
imagesc(s);
ax = gca;
% Replace the original yTick values of the `imagesc` plot.
yTicks = ax.YAxis.TickValues;
tickDifference = yTicks(2) - yTicks(1);
for ii = 1:length(yTicks)
yTicks(ii) = size(s,1) - tickDifference * (ii - 1);
end
yTicks = sort(yTicks);
ax.YAxis.TickValues = yTicks;
% Replace the ticklabel values to match the flipped axis.
yTickLabels = ax.YAxis.TickLabels;
scaling = 10^double(ax.YAxis.Exponent); % Take care of scaling
for ii = 1:length(yTickLabels)
yTickLabels{ii} = num2str(size(s,1) - str2double(yTickLabels{ii})*scaling);
end
ax.YAxis.TickLabels = yTickLabels;
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations
Find more on Annotations in Help Center and File Exchange
Tags
- scatter
- 2d plots
- ticks
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office