How to change the linewidth in a figure before actually plotting some? (2024)

314 views (last 30 days)

Show older comments

Ruben on 2 Dec 2014

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some

Answered: Salvador Castaneda on 23 Oct 2021

Accepted Answer: dpb

Open in MATLAB Online

This question maybe a bit like the link below, but this didn't work for me... http://nl.mathworks.com/matlabcentral/answers/102530-how-can-i-change-the-default-settings-for-the-linewidth-property-before-i-plot-a-figure-in-matlab

I'm working on a matlab function that automatically opens your figure in full screen mode and on a second monitor if present. So far, everything works fine. I already achieved to set the fontsize inside the function, so whitout plotting anything and without making xlabel(..) etc.:

% Fontsize used at the figure

if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)

set(gca,'FontSize',16)

else

set(gca,'Fontsize',fontsize_manual)

end

Now is my question: Can I change in a same way the linewidth of the lines that will by plotted in the figure? So also here, predefining the linewidth inside the function and later on in your script plotting some lines etc. I do prefer this works only for the figure you're working on, so that you can change this 'default' for each figure and save them all with different linewidth and fontsizes if needed.

I tried the line below, but that only changed the linewidth of the axis.

set(gca,'LineWidth',2)

Is there anyone who can help me solving this problem?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

dpb on 2 Dec 2014

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_161009

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_161009

Open in MATLAB Online

Use the technique of the referenced page but set the default for the figure in question --

set(gca,'DefaultLineLineWidth',2)

NB: the default properties are named in run-on fashion--the name of the object you're setting then the property within that object all preceded with the keyword 'Default'. Here that's

Default:Line:LineWidth

to separate the subsets visually. I think it would've been easier if TMW had used such nomenclature or a structure form instead (and probably the parsing internally would've been simpler as well).

9 Comments

Show 7 older commentsHide 7 older comments

Ruben on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253587

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253587

That was the only one I did not tried. This actually worked for the first figure and not for the second time I call the function in a matlab session. However, changing 'gca' into 'gcf' gave the required result for both figures! Thanx!

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253591

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253591

Open in MATLAB Online

But now I run into a new problem... The code below in found accidentally by solving the previous problem:

set(gca,'LineWidth',3)

It turned out this changes the width of the axes. But now the problem... Also here this works only on the first figure. (see figure)

How to change the linewidth in a figure before actually plotting some? (5)

If I also put this code in my session after the plotting in the second figure, the width in the second figure changes. Looks like the right handle isn't reached, or something like that, inside the function, when making the second figure. Do you have any idea what could be wrong here?

Ruben on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253611

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253611

Open in MATLAB Online

same holds for:

set(gca,'FontName','Arial')

dpb on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253617

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253617

Yes, you've got to be certain you're looking at the right object-- gca and gcf are what they say--the CURRENT axes or figure which may (or may not) actually be the one you're aiming to modify when your code makes the call.

When doing stuff like this you need to always save the handles of the objects when created and use those to ensure you modify the desired one.

How to solve the above problem specifically would depend on seeing the code that generated the figures in sequence and in context.

Ruben on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253624

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253624

Open in MATLAB Online

This will be (a part of) the plotting in the test-session...

figuur1=fancy_figure('open','figuur1',[],[],[],[]);

lijn1=plot(x(4:5),[epsilon(4) epsilon(4)],'k');%,'linewidth',2);

figuur2=fancy_figure('open','figuur2',[],[],[],5);

plot(x(4:5),[epsilon(4) epsilon(4)],'k')

And this will be the function that I seperately saved as fancy_figure.m

function [figure_handle]= fancy_figure(action,figure_handle,...

save_name,save_format,fontsize_manual,linewidth_manual)

windows 7 tested and matlab R2014a

%%Function code for plotting full screen figure and do this on second monitor if present.

% If the 'action' input argument equals 'save' the figure will be saved

% with the settings requested.

switch action

case 'open'

%%Open a figure on full screen and on the second monitor if present.

% Format of monitor_positions in pixels: [xMin yMin xMax yMax]

mon_pos = get(0,'MonitorPositions');

size_monitor = size(mon_pos);

if (size_monitor(1) > 1)

figure_handle = figure('Position', [mon_pos(2,1:2) 50 50]);

pause(0.1);

set(get(handle(gcf),'JavaFrame'),'Maximized',1);

else

figure_handle=figure;

pause(0.01);

set(get(handle(gcf),'JavaFrame'),'Maximized',1);

end

% Fontsize used at the figure

if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)

set(gca,'FontSize',16)

set(gca,'FontName','Dotum')

else

set(gca,'Fontsize',fontsize_manual)

set(gca,'FontName','Dotum')

end

dpb on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253690

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253690

Edited: dpb on 6 Dec 2014

Open in MATLAB Online

Well, your code is rife with what I said specifically to not do... :)

...

if (size_monitor(1) > 1)

figure_handle = figure('Position', [mon_pos(2,1:2) 50 50]);

pause(0.1);

set(get(handle(gcf),'JavaFrame'),'Maximized',1);

...

Here you just created a figure and save its handle but then turn around and use gcf instead of that specific handle. While this particular one is going to be ok since they are inline, it's poor practice.

On down just a little ways, the source of your problem is

% Fontsize used at the figure

if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)

set(gca,'FontSize',16)

set(gca,'FontName','Dotum')

...

where you have created a figure but there is no axes object on it as yet. Hence gca gets the last axis that was drawn upon, not the one that will (eventually, presumably) be created on that figure.

I repeat, you have to be sure you're addressing the object you want...

dpb on 4 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253732

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253732

Open in MATLAB Online

And, the calling sequence you show of

figuur1=fancy_figure('open','figuur1',[],[],[],[]);

lijn1=plot(x(4:5),[epsilon(4) epsilon(4)],'k');%,'linewidth',2);

figuur2=fancy_figure('open','figuur2',[],[],[],5);

...

causes the first figure to be opened, then an axes is created and plotted onto with plot prior to the subsequent call to fancy_figure. At that point, that is the current axis and the handle which gca will return...

Ruben on 4 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253943

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253943

That was indeed the kind of code badness I expected that would give the 'strange' result and I slowly get your explanation. Maybe I have some time left this weekend XD

Do you get the point, what I want to reach with this function? If yes, do you have a smart idea or can you explain short what I maybe can do better, more careful etc?

dpb on 4 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253954

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253954

Edited: dpb on 6 Dec 2014

Open in MATLAB Online

Primarily it looks like everywhere you have a gca you really want something that goes back to your original question and to probably try to set defaults for the just-created figure using

set(figure_handle,'DefaultLineLineWidth',2)

or the desired property/properties on the various other objects besides Line. You'll have to investigate which are and are not inherited from the figure; I don't know that otomh. In the initial query the linewidth property is; I've not investigated for the various font properties from whom they get derived. But, if you can't get to them from the figure then you'll have to have another level at which you create and save the right handles for the axes if you do want to make these dependent on specific figures and axes within those figures rather than global.

It's all doable; you just have to figure out where is the highest point in the object hierarchy you can do so. Try the figure first, obviously, but as soon as you use gca you've gone to an axes level, not figure so you'll have to see if it works substituting gcf first, then switch to the actual figure. If gcf fails because of invalid property then you've got to make the axes object and use (and save) that handle that's tied with the given parent figure.

Sign in to comment.

More Answers (2)

Robert Cumming on 3 Dec 2014

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_161147

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_161147

Open in MATLAB Online

set(0,'DefaultLineLineWidth',2)

This will set the line width for all plots in the current session

The technique your using in @dpb answer only sets the current axes (i.e. gca).

1 Comment

Show -1 older commentsHide -1 older comments

Ruben on 3 Dec 2014

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253602

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#comment_253602

Yeah, but I would have the freedom to choose the linewidth of the plotted object per figure... (A) So the answer @dpb is more suitable for my problem.

Sign in to comment.

Salvador Castaneda on 23 Oct 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_814653

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/165149-how-to-change-the-linewidth-in-a-figure-before-actually-plotting-some#answer_814653

This however, was great for mine, thanks

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsGraphics ObjectsGraphics Object Programming

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

  • linewidth figure function

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to change the linewidth in a figure before actually plotting some? (16)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to change the linewidth in a figure before actually plotting some? (2024)

FAQs

How do you change the LineWidth of a plot in Python? ›

Setting the line width

You can also change the line width by passing a linewidth parameter to the plt. plot() function. The linewidth parameter takes a floating-point value representing the line's width.

How to change LineWidth in MATLAB plot? ›

Specify the line width by setting the “LineWidth” property a name-value pair.
  1. plot(x1,y1,'LineWidth',5)
  2. hold on.
  3. plot(x2,y2,'LineWidth',10)
  4. hold off.
Sep 22, 2011

What is LineWidth in plot? ›

The line width simply helps to give a unique description or visual to a particular data set in a plot. The default value for the line width of a plot in Matplotlib is 1 . However, you can change this size to any size of your choice.

How to change the width of a plot in MATLAB? ›

plot(X_a,Y_a,'LineWidth',10);

How do you change the width of a plot in Python? ›

If you only want to change the height or width of a figure, you can use set_figwidth() or set_figheight() . By default, an inch is 100 pixels in length. You can use set_dpi() to change this.

How do you change the width of an image in Python? ›

Call the resize() method on the new image instance, passing a tuple argument with two integers to specify the width and height you desire:
  1. image = Image. open('myimage. jpg')
  2. new_image = image. resize((500, 500))
  3. new_image. save('myimage_500. jpg')

Why do we use LineWidth in MATLAB? ›

For example, Line objects have a LineWidth property for controlling the line's thickness. To create a thicker line, you can specify the LineWidth as a name-value argument when you call the plot function. In this case, set the LineWidth to 3 . Return the Line object as p so you can set other properties later.

What is the default LineWidth of a plot in Matplotlib? ›

If you want to reset the default linewidth to its original value, you can simply set it back to the default value of 1.

How do I change line spacing in MATLAB? ›

To change the line spacing option, do one of the following: On the Home tab, in the Environment section, click Preferences. Select MATLAB > Command Window, and then choose a Line spacing option.

What is the default linewidth in MATLAB? ›

The plot above uses the default MATLAB line width of 0.5 points. Here's where I have to wave my hands a little. Because of the way the figure above was captured for display in your browser, the lines probably appear a little thicker than 0.5 points.

How do you calculate linewidth? ›

In frequency units, the line width is ΔU=ΔE/h=1/τ (τ is called the relaxation time). If τ is very small, then the line is strongly broadened and will not be observed experimentally. On the other hand if τ is too large, then the system that has absorbed the energy will not have time to reach its Boltzman equilibrium.

What is linewidth or Textwidth? ›

\textwidth is the normal width of the text on a page. It should generally be changed only in the preamble. \linewidth is the width of lines in the current environment. Normally equal to \textwidth, it may be different within an environment such as list or quote environments.

How do you edit a plot in MATLAB? ›

To change the properties of a plot, first click the Send to Figure button in the toolstrip to open a new figure window and then use any of the buttons in the plot editing toolbar. Analysis Parameters contains parameter settings applicable to the displayed analysis.

How do you change the width of a chart? ›

To resize a chart, do one of the following: To change the size manually, click the chart, and then drag the sizing handles to the size that you want. To use specific height and width measurements, on the Format tab, in the Size group, enter the size in the Height and Width box. .

How do you change the ratio of a plot in MATLAB? ›

By default, the plot box aspect ratio is based on the size of the figure. You can change the aspect ratio using the pbaspect function. Set the ratio as a three-element vector of positive values that represent the relative axis lengths.

How to make a line bold in Python? ›

How do you write bold in Python? To print the bold python text, use the built-in ANSI escape sequences for making text bold, italic, or colored, etc. By suing the particular ANSI escape sequences, the text can be printed in different formats. The ANSI escape sequence to print bold text in Python is: '/033[1m'.

How do I change the axis scale in Python? ›

The simplest way to set the axis range in Matplotlib is by using the xlim() and ylim() functions. These functions allow you to define the minimum and maximum values that will be displayed on the X and Y axes, respectively. In the above example, we've set the X-axis to range from 0 to 5 and the Y-axis from 0 to 20.

How do you increase the width of a plot? ›

You can use the set_figwidth() method to change the width of a plot. We'll pass in the value the width should be changed to as a parameter to the method. This method will not change the default or preset value of the plot's height.

Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 5370

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.