1

I'm doing some plots in Matlab 2014b, in one them I'm plotting 5 variables versus time over there and want to specify 5 names like, speed (rad/sec), voltage (V), ... etc. on the y-label, however, as you can see in the picture, I'm getting extra bland space on the left, and struggling to removed it.Sample figure

Here is a working code, just to explain the situation:

clear all; clc; close all;
x = rand(8,1);
y = rand(8,1);

figure
plot(x,y)
xlabel('time (s)')
ylabel('$x$ (m), $\theta$ (rad), $\dot{x}$ (m/s),$\dot{\theta}$ (rad/s), $\dot{S}_{gen}$(W/K)','Interpreter', 'latex','fontsize',10)
grid

Any suggestions are highly appreciated in advance.

6
  • 1
    Which version of Matlab? I don't have that problem on 2018b with the code you've provided: imgur.com/CktT8ME The plot has the following (default for me) settings: pastebin.com/00GFw7X3 – Saaru Lindestøkke Dec 5 '18 at 10:07
  • I'm using Matlab 2014b – AlFagera Dec 5 '18 at 10:10
  • Ok, I don't have access to that version. Perhaps you can take a look at my plot settings and compare them to yours to see if there's a difference there? – Saaru Lindestøkke Dec 5 '18 at 10:13
  • Better yet: after you've created your plot, run the command gcf and post it's output (make sure to show all properties) in your questions so we can take a look at it. – Saaru Lindestøkke Dec 5 '18 at 10:35
  • Running gcf almost produce nothing, only a blank figure wizard is shown and ans = 1 is displayed in the command window – AlFagera Dec 5 '18 at 12:41
1

You can tweak the positioning of the axes in the figure window as follows (using your example code):

clear all; clc; close all;
x = rand(8,1);
y = rand(8,1);

figure;
ax = handle(gca);
ax.Position = [0.1, 0.15, 0.8, 0.75];
plot(x,y)
xlabel('time (s)')
ylabel('$x$ (m), $\theta$ (rad), $\dot{x}$ (m/s),$\dot{\theta}$ (rad/s), $\dot{S}_{gen}$(W/K)','Interpreter', 'latex','fontsize',10)
grid

Explanation of the code

In ax = handle(gca); you store the axes handle in variable ax such that you can alter it later on.

In ax.Position [0.1, 0.15, 0.8, 0.75]; you change the position of the axes in the figure referred by handle ax. The example above sets the lower left point at the coordinates 0.1, 0.15 and a width and height of 0.8 and 0.75 respectively. These coordinates are normalized w.r.t. to your figure size.

Example plots

Initial situation

If you set the ax.Position to [0.25, 0.15, 0.7, 0.75] you get something like you had initially:

enter image description here

Solution

If you set the ax.Position to [0.1, 0.1, 0.85, 0.85] you get far less whitespace:

enter image description here

Notes

This MATLAB help link explains how you can define the axes position "automatically" such that there is a tighter fit.
This MATLAB help link on axes explains what parameters are available in the axes object.
This answer over at Stackoverflow helped me find a way to use the dot notation in MATLAB versions prior to 2014b. Apparently using handle() is also faster than the set and get methods.

5
  • Belief it or not, using this command h.CurrentAxes.Position = [0.1, 0.1, 0.75, 0.75]; I got no change in my plot, even when I set: h.CurrentAxes.Position = [0.01, 0.01, 0.5, 0.5]; Only I got this warning: "Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.". however, the links you provided have helped me to solve the problem, especially the 2nd link It turned out I only need to add this command ax1 = axes('Position',[0.15 0.15 0.75 0.75]);after figure – AlFagera Dec 5 '18 at 14:57
  • Nice to hear that your problem is solved. I think the error you receive is due to a mismatch of our Matlab versions. Apparently you cannot use the dot notation in your version of Matlab to assign the figure properties. Are you sure you use version 2014b? This question is about the error you see. – Saaru Lindestøkke Dec 5 '18 at 15:54
  • Yes, I'm sure of my 2014b version, actually its stated there that: "Starting in R2014b, you can use dot notation to query and set properties. If you are using an earlier release, use the get and set functions instead." as the question you share: Accessing graphic object properties using dot notation on Matlab versions anterior to (before) R2014b. I would suggest that you modify your answer such that I can mark it as accepted h = figure; ax1 = axes('Position',[0.15 0.15 0.75 0.75]); plot() – AlFagera Dec 5 '18 at 16:12
  • I've modified it. – Saaru Lindestøkke Dec 5 '18 at 20:14
  • Ok, though I do prefer ax.Position = [0.15, 0.15, 0.75, 0.75]; – AlFagera Dec 6 '18 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.