Setting default line widths for all plotting functions

조회 수: 379 (최근 30일)
Roy Goodman
Roy Goodman 2023년 8월 16일
편집: Adam Danz 2023년 8월 16일
MATLAB's default linewidths are all too narrow for my taste. I have a startup.m file with many lines like
set(groot,'DefaultLineLineWidth',2)
set(groot,'DefaultContourLineWidth',1.5)
set(groot,'DefaultFunctionContourLineWidth',2)
Every time I learn about a new plot type, I have to add an additional line to my startup file. Today I needed to use fimplicit. So far, I haven't figured out what to set to fix this. This leads me to three questions.
  1. What's the line I need to add to my startup file?
  2. How would I reverse engineer this to find out without posting here?
  3. Is there a master linewidth that I could set which would set all line widths for all time, no matter what function was used to create them?

채택된 답변

Steven Lord
Steven Lord 2023년 8월 16일
Looking at your second question first, if you look at the end of the fimplicit function's documentation page, there's a link to "ImplicitFunctionLine Properties". On that page, the value of the Type property is 'implicitfunctionline'. By the "Specify Default Values" section on this documentation page that means you should set the property:
P = "default" + "implicitfunctionline" + "LineWidth"
P = "defaultimplicitfunctionlineLineWidth"
This sort of implicitly answers your first question. Let's try it.
h = fimplicit(@(x,y) x.^2 - y.^2 - 1);
d = h.LineWidth % show the factory default
d = 0.5000
title("LineWidth of " + d)
set(groot, P, 10*d);
figure
h = fimplicit(@(x,y) x.^2 - y.^2 - 1);
d = h.LineWidth % show the factory default
d = 5
title("LineWidth of " + d + " after changing default")
For your third question I believe the answer is no. However, if you're okay with changing the properties of all objects with a LineWidth property after the fact, you could use the findall or findobj functions and ask for all objects that have a LineWidth.
h = findall(groot, ... % All graphics objects that are "descendants" of groot (i.e. all of them)
'-property', 'LineWidth') % that possess a LineWidth property
h =
6×1 graphics array: Axes (LineWidth of 5 after changing default) Axes (LineWidth of 0.5) ImplicitFunctionLine Text (LineWidth of 5 after changing default) ImplicitFunctionLine Text (LineWidth of 0.5)
Note that in addition to the lines created by fimplicit h lists two axes (which the axes properties page states controls "Line width of axes outline, tick marks, and grid lines, specified as a positive numeric value in point units. One point equals 1/72 inch.") and two text objects (the titles, for which the text properties page says the LineWidth property controls "Width of box outline, specified as a scalar numeric value in point units. One point equals 1/72 inch.")
So let's look at what would happen if you changed all those LineWidths to something much larger. It might not look exactly as you want, since the axes has a LineWidth.
f = figure;
h = fimplicit(@(x,y) x.^2 - y.^2 - 1);
t = title('Example');
titleLineWidthBefore = t.LineWidth
titleLineWidthBefore = 0.5000
hasLineWidth = findall(f, '-property', 'LineWidth')
hasLineWidth =
3×1 graphics array: Axes (Example) ImplicitFunctionLine Text (Example)
set(hasLineWidth, 'LineWidth', 10)
The LineWidth change did affect the text object returned by title, but since that object's EdgeColor is 'none' we don't see any visible change.
t.EdgeColor
ans = 'none'
titleLineWidthAfter = t.LineWidth
titleLineWidthAfter = 10
  댓글 수: 1
Roy Goodman
Roy Goodman 2023년 8월 16일
This answer is thorough beyond any expectation. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2023년 8월 16일
편집: Adam Danz 2023년 8월 16일
This issues comes up from time to time. As @Steven Lord mentioned, there is no global setting that would apply to all LineWidth properties.
Here's an alternative approach inspired by the work of another user who recently faced this problem. I've generalized and slightly improved the solution below.
It makes the following assumptions
  1. All LineWidth properties will be listed in the groot factory list
  2. All LineWidth properties can be identified as ending in "LineWidth" as Steven Lord explained.
  3. All factory properties ending in LineWidth affect the LineWidth property of an object and can accept a value of 1.
  4. All LineWidth factory properties can be converted to default property names
This was tested in R2023a which lists 46 linewidth names
% List all factory properties
factoryNames = fieldnames(get(groot,'factory'));
% Identify and extract the factory properties that end with "LineWidth"
factoryLineWidthIndex = endsWith(factoryNames,'LineWidth');
factoryLineWidthNames = factoryNames(factoryLineWidthIndex)
factoryLineWidthNames = 46×1 cell array
{'factoryAnimatedlineLineWidth' } {'factoryAreaLineWidth' } {'factoryArrowshapeLineWidth' } {'factoryAxesGridLineWidth' } {'factoryAxesLineWidth' } {'factoryAxesMinorGridLineWidth' } {'factoryBarLineWidth' } {'factoryBubblechartLineWidth' } {'factoryBubblelegendLineWidth' } {'factoryCategoricalhistogramLineWidth' } {'factoryColorbarLineWidth' } {'factoryConstantlineLineWidth' } {'factoryConstantregionLineWidth' } {'factoryContourLineWidth' } {'factoryDoubleendarrowshapeLineWidth' } {'factoryEllipseshapeLineWidth' } {'factoryErrorbarLineWidth' } {'factoryFunctioncontourLineWidth' } {'factoryFunctionlineLineWidth' } {'factoryFunctionsurfaceLineWidth' } {'factoryGeoaxesLineWidth' } {'factoryGraphplotLineWidth' } {'factoryHistogram2LineWidth' } {'factoryHistogramLineWidth' } {'factoryImplicitfunctionlineLineWidth' } {'factoryImplicitfunctionsurfaceLineWidth'} {'factoryLegendLineWidth' } {'factoryLineLineWidth' } {'factoryLineshapeLineWidth' } {'factoryMapaxesGraticuleLineWidth' }
% Convert the factory property list to default property names
defaultLineWidthNames = regexprep(factoryLineWidthNames,'^factory','default')
defaultLineWidthNames = 46×1 cell array
{'defaultAnimatedlineLineWidth' } {'defaultAreaLineWidth' } {'defaultArrowshapeLineWidth' } {'defaultAxesGridLineWidth' } {'defaultAxesLineWidth' } {'defaultAxesMinorGridLineWidth' } {'defaultBarLineWidth' } {'defaultBubblechartLineWidth' } {'defaultBubblelegendLineWidth' } {'defaultCategoricalhistogramLineWidth' } {'defaultColorbarLineWidth' } {'defaultConstantlineLineWidth' } {'defaultConstantregionLineWidth' } {'defaultContourLineWidth' } {'defaultDoubleendarrowshapeLineWidth' } {'defaultEllipseshapeLineWidth' } {'defaultErrorbarLineWidth' } {'defaultFunctioncontourLineWidth' } {'defaultFunctionlineLineWidth' } {'defaultFunctionsurfaceLineWidth' } {'defaultGeoaxesLineWidth' } {'defaultGraphplotLineWidth' } {'defaultHistogram2LineWidth' } {'defaultHistogramLineWidth' } {'defaultImplicitfunctionlineLineWidth' } {'defaultImplicitfunctionsurfaceLineWidth'} {'defaultLegendLineWidth' } {'defaultLineLineWidth' } {'defaultLineshapeLineWidth' } {'defaultMapaxesGraticuleLineWidth' }
% Loop through the default line width names and set their value to 1
for i = 1:numel(defaultLineWidthNames)
set(groot,defaultLineWidthNames{i},1)
end
Test it
fig = figure();
ax = axes();
hold on
h = plot([1,2]);
xl = xline(1.5);
cb = colorbar();
h.LineWidth
ans = 1
xl.LineWidth
ans = 1
cb.LineWidth
ans = 1
ax.LineWidth
ans = 1
close(fig)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by