Changing Some MATLAB Plotting Defaults

Steve Eddins on 10 Mar 2025
Latest activity Reply by Stephen23 on 15 Apr 2025 at 21:13

Over the last 5 years or so, the highest-traffic post on my MATLAB Central image processing blog was not actually about image processing; it was about changing the default line thickness in plots.
Now I have written about some other MATLAB plotting behavior that I have recently changed to suit my own preferences. See this new blog post.
Here is a standard MATLAB plot:
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
I don't like some aspects of this plot, and so I have put the following code into my startup file.
set(groot,"DefaultLineLineWidth",2)
set(groot,"DefaultAxesXLimitMethod","padded")
set(groot,"DefaultAxesYLimitMethod","padded")
set(groot,"DefaultAxesZLimitMethod","padded")
set(groot,"DefaultAxesXGrid","on")
set(groot,"DefaultAxesYGrid","on")
set(groot,"DefaultAxesZGrid","on")
With those defaults changed, here is my preferred appearance:
plot(x,y1,x,y2)
goc3
goc3 on 11 Mar 2025
Results of my margins poll indicate a fairly even split between users that like the margins as is and those who think that they are too large:
I also think that many aspects of the default figure are not ideal.
Perhaps the best way forward would be for MathWorks to develop three to five default plot styles that get shipped with MATLAB. That way, each user could pick one of the available graphics themes in the settings. (Beyond what is possible with the Figure Copy Template in preferences.)
As it is, I often spend a lot of time modifying figure details and would much prefer to use a built-in theme.
Yair Altman
Yair Altman on 10 Mar 2025
A long-time small annoyance I have with the default/factory settings mechanism is the confusing labels such as "DefaultLineLineWidth" that I find quite user-non-friendly.
A better approach (IMHO) would have been to support dot-seperated values, as in:
set(groot,"Default.Line.LineWidth",2)
Perhaps this could be retroffitted into MATLAB, by having the engine ignore in-string dots in the setter method, and generate them in the getter. Just a thought....
Steve Eddins
Steve Eddins on 11 Mar 2025
I like it.
Michelle Hirsch
Michelle Hirsch on 11 Mar 2025
I see the appeal in this but I have significant concerns about such a special case behavior because adding the dots makes this name not a valid MATLAB identifier. One obvious consequence is that named argument syntax wouldn't work, as it does automatically everywhelse else name-value arguments are supported in MATLAB:
set(groot,Default.Line.LineWidth = 2)
I'm not saying that it's essential that named argument syntax work specifically for setting defaults, but I do believe that we need to minimize special case/one off behaviors because they can make the system overall harder to reason on.
You could make the case that we've already got special case behavior with defaults, because it's the only place where the name passed to set isn't precisely a property name. That was a decision made long, long, ago, based on the capabilities of the graphics system and the language at the time (before proper graphics objects). If we wanted to really tackle this, we'd probably design something from scratch using the language features we have today. Maybe it would go into settings:
s = settings;
s.graphics.defaults.Line.LineWidth = 2;
Or maybe we'd set properties on groot (my preference), like:
gr = groot;
gr.Default.Line.LineWidth = 2;
Stephen23
Stephen23 on 15 Apr 2025 at 21:13
Underscore?
Steve Eddins
Steve Eddins on 11 Mar 2025
That all makes good sense, Michelle. I like your design sketches. I can imagine that a project like this wouldn't get a high priority unless it became part of a larger design effort with broader impact.
Walter Roberson
Walter Roberson on 10 Mar 2025
I am a bit puzzled that the yticks changed ?
Steve Eddins
Steve Eddins on 10 Mar 2025
If you look at the corresponding plots in my blog post, you can see that the y ticks are the same there, with both showing a tick spacing of 0.5. I believe that is because I generated those plots in the Live Editor, which uses a smaller backing figure, I believe. We are just seeing some kind of spacing threshold at play in the tick picker.
Steve Eddins
Steve Eddins on 10 Mar 2025
Because of the padding above and below -1 and 1, there is slightly less space between -1 and 1 on the y-axis in the second plot, and that affected the tick-picking algorithm. The original set of ticks and labels would have gotten a bit crowded.