Adding fractions to axis tick marks

조회 수: 26 (최근 30일)
Mitchell Frechette
Mitchell Frechette 2016년 11월 1일
댓글: Erik RF 2021년 3월 20일
How do you turn the tick mark labels into fractions rather than decimals. Take the following code for example. I would like the tick mark to remain in their current location but display their associated fraction instead.
x=.1:.1:.5
y=1:5
clf
plot(x,y,'color','r','linewidth',2)
set(gca, 'XTick', [1/16 1/8 3/16 1/4 5/16 3/8 7/16 1/2])

채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 1일
If you are using R2014b or later, you
set(gca, 'TickLabelInterpreter', 'latex', 'XTickLabel', {'$\frac{1}{16}$', '$\frac{1}{8}$', '$\frac{3}{16}$', '$\frac{1}{4}$', '$\frac{5}{16}$', '$\frac{3}{8}$', '$\frac{7}{16}$', '$\frac{1}{2}$'})
If you are using an earlier version, you could go through some trouble to text() the individual labels in place, because in those earlier versions, individual text() could have an interpreter but TickLabelInterpreter could not be set. So in those earlier versions it would probably be easier to
set(gca, 'XTickLabel', {'1/16' '1/8' '3/16' '1/4' '5/16' '3/8' '7/16' '1/2'})
  댓글 수: 1
Mitchell Frechette
Mitchell Frechette 2016년 11월 1일
Thank you! This worked just how I wanted.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2018년 10월 31일
Is it possible to find a soft-code this? So instead of
set(gca, 'TickLabelInterpreter', 'latex', 'XTickLabel', {'$\frac{1}{16}$', '$\frac{1}{8}$', '$\frac{3}{16}$', '$\frac{1}{4}$', '$\frac{5}{16}$', '$\frac{3}{8}$', '$\frac{7}{16}$', '$\frac{1}{2}$'})
I would like to go for a version where
a=[16 8 4 2 1]
set(gca, 'TickLabelInterpreter', 'latex', 'XTickLabel', {'$\frac{1}{a}$'})
So basicly my question is, how to load a variable while writing in a math mode of latex.
  댓글 수: 1
Erik RF
Erik RF 2021년 3월 20일
Yes, try this.
val = 0:0.25:10; % your vector of values in decimal form
[num, den] = rat(num); % convert to rational form
ticks = '$\frac{'+string(enu)+'}{'+string(den)+'}$';
set(gca, 'TickLabelInterpreter', 'latex', 'XTickLabel', ticks)
You can also add more info to each label if you want, for example add pi like this:
ticks = '$\frac{'+string(enu)+'}{'+string(den)+'}\pi$';

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by