How to show fractional values in plot

7 次查看(过去 30 天)
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by