how to plot along x-axis, given objects width (w) = 5cm, 10cm, 20cm! on the same graph

3 views (last 30 days)
how to plot along x-axis, given objects width (w) = 5cm, 10cm, 20cm! on the same graph
  4 Comments
Stephen23
Stephen23 on 7 May 2015
And very rude to the person who volunteered their own time to help you: they spent time and effort thinking, developing and testing... but on the understanding that this is a public resource that is a repository for other users to look-up too.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 May 2015
It is tricky to graph something to come out a given screen width, but it can sort of be done.
But since all of the objects need to be on the x-axis, you are going to need your axes to be at least 5+10+20 = 35 cm wide, about 13 3/4 inches; you also need some drawing room around the axes. You would probably need a monitor at least 14 1/4 inches wide.
Are you sure that the objects have to display that width on the screen ? MATLAB's method of calculating how wide something will appear on the screen are based upon assumptions that are often not true. For example on CRT monitors MATLAB cannot find out how much the user has fiddled with the monitor controls to expand or tighten how the beam hits the phosphors.
Anyhow, to start you off:
figno = figure('Units', 'normal', 'Position', [0 0 1 1]); %nominally full screen, probably not really
axno = axes('Parent', figno, 'Units', 'normal', 'OuterPosition', [0 0 1 1]); %position axes within figure
The above two can be replaced with whatever code is already used to establish the axes
The code I give here, below, assumes that the axes has nothing in it already, that all you want to do is draw the three objects. If you already have data, a variation on the below could be used to figure out what is already there and work with it
Assuming now that we can configure the axes for our own convenience
set(axno, 'XLim', [0 1], 'YLim', [0 1]);
set(axno, 'Units', 'cm');
cm_pos = get(axno, 'Position'); %Position reflects the location of the x and y axis lines
xwidth = cm_pos(3);
if xwidth < 5+10+20 %third component is x width
disp('Cannot plot all three objects on the x-axis, there is not enough room for them side by side';
return; %or error out, or whatever
end
one_cm_as_fraction = 1.0 / xwidth; %the 1.0 ties directly to the xlim [0 1]
cm_5_as_fraction = 5 * one_cm_as_fraction;
cm_10_as_fraction = 10 * one_cm_as_fraction;
cm_20_as_fraction = 20 * one_cm_as_fraction;
cm_5_start = 0.0; %the 0.0 ties directly to the xlim [0 1]
cm_5_end = cm_5_start + cm_5_as_fraction;
cm_10_start = cm_5_end; %10 cm bar starts right after 5 cm bar
cm_10_end = cm_10_start + cm_10_as_fraction;
cm_20_start = cm_10_end; %20 cm bar starts right after 10 cm bar
cm_20_end = cm_20_start + cm_20_as_fraction;
plot([cm_5_start cm_5_end], [0 0], 'r'); %5 cm bar in red
plot([cm_10_start cm_10_end], [0 0], 'g'); %10 cm bar in green
plot([cm_20_start cm_20_end), [0 0], 'b'); %20 cm bar in blue
I still suspect that what you really want to do is not what you asked, but this is how to do what you asked.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!