Nicholas Kinar
Nicholas Kinar

Reputation: 1470

Adjusting size of plot in Matlab so that graph does not get cut off by edge of plot window

I've created a plot in Matlab, but unfortunately the side of the plot is cut off by the plotting window. Here is the code that I've used to create the plot:

% create a plot with dots and with bold sides
point = num(:, 1);
depth = num(:, 2);
hfig = plot(point, depth, '-s', 'Color', 'k', 'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k', 'MarkerSize', 10);

% make the outside of the graph bold
set(gca, 'LineWidth', axis_size);
set(gca,'FontSize', ticksize, 'FontName', fontname);
set(gca, 'Position', [0.2 0.2 0.8 0.4])
xlabel('Point Number')
ylabel('Depth (cm)')

Here is the output:

Cutoff image

Notice how the side of the plot has been cut off. Alternately, here is a picture showing what I want to achieve:

What I want to achieve

I simply created the plot without the call to set(gca, 'Position', [0.2 0.2 0.8 0.4]), and then resized the plot by resizing the window.

But how do I do this in code? I also want to ensure that I can save out the plot as a PNG file with the same dimensions. I've been playing around with various combinations of the set() function, and I've been unable to attain what I want to get.

Upvotes: 2

Views: 29721

Answers (3)

Keegan Keplinger
Keegan Keplinger

Reputation: 627

Andrey has the simplest answer, but alternatively (and for the long term) you can go to the file exchange and download subaxis.m, which replaces subplot, and is superb for even single axes plots (you can define properties like LeftMargin and RightMargin and PaddingTop and PaddingBottom)

http://www.mathworks.com/matlabcentral/fileexchange/3696-subaxis-subplot

Upvotes: 1

Nicholas Kinar
Nicholas Kinar

Reputation: 1470

The following document (How to produce figures with Matlab that have the right dimensions when printed) provides some example code that can be used to resize the plot in the proper fashion.

The following code was used:

point = num(:, 1);
depth = num(:, 2);

hfig = plot(point, depth, '-s', 'Color', 'k', 'MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k', 'MarkerSize', 10);
set(gca, 'LineWidth', axis_size);
set(gca,'FontSize', ticksize, 'FontName', fontname);
set(gcf,'PaperUnits','centimeters')
xSize = 30; ySize = 5;
xLeft = (21-xSize)/2; yTop = (30-ySize)/2;
set(gcf,'PaperPosition',[xLeft yTop xSize ySize])
set(gcf,'Position',[300 600 xSize*50 ySize*50])

xlabel('Point Number');
ylabel('Depth (cm)');  

Here is a picture of the result. This looks similar to the one that I posted in my question above. I don't know if this is dependent on monitor size and/or screen resolution.

Expected result

Upvotes: 0

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

Try to set the OuterPosition of the plot.

set(gca,'OuterPosition',[0.2 0.2 0.8 0.4]);

The image below shows the difference between the two:

enter image description here

Here is the relevant MathWorks help page.

Upvotes: 5

Related Questions