How to show fractional values in plot

How do you show fractional values in plot for the x or y axis instead of decimal values.
For example show 1/298 instead of 3.3557e-3
I know you can manually label the axis but I dont know the end points. Is there a way to do this?
Thanks,
C

1 件のコメント

Image Analyst
Image Analyst 2012 年 10 月 6 日
You can do this to get the existing tick positions, including the end point ticks:
% Get existing tick marks.
existing_Y_Ticks = get(gca, 'YTick')

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

 採用された回答

Image Analyst
Image Analyst 2012 年 10 月 6 日

0 投票

Try this:
% Generate some sample data and plot it.
m = .04 * rand(20, 1);
plot(m);
grid on;
% Now convert the existing tick marks.
% Get existing tick marks.
existing_Y_Ticks = get(gca, 'YTick')
% Define the denominator we want to use.
denominator = 298;
% Find out what the numerators would be.
New_Y_Tick_Numerators = (existing_Y_Ticks * denominator)
% Now make up a cell array of all the tick labels.
for k = 1 : length(existing_Y_Ticks)
y_tick_labels{k} = sprintf('%.1f / %d', New_Y_Tick_Numerators(k), denominator);
end
% Apply our tick marks to the plot.
set(gca,'YTickLabel',y_tick_labels);

2 件のコメント

Walter Roberson
Walter Roberson 2012 年 10 月 6 日
If you do not know the denominator then you may wish to use the function to convert to rational numbers
Image Analyst
Image Analyst 2012 年 10 月 6 日
You mean rats()?
I suppose Mike could also change the tick marks to new values instead of using existing values, so maybe instead of having 5 tick marks with fractional values in the numerator like 1.1/298, 2.2/298, etc. you could have 6 or 7 tick marks with integer multiples of 1/298.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by