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

530 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Ruben am 2 Dez. 2014

  • Verknüpfen

    Direkter Link zu dieser Frage

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

  • Verknüpfen

    Direkter Link zu dieser Frage

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

Beantwortet: Salvador Castaneda am 23 Okt. 2021

Akzeptierte Antwort: dpb

In MATLAB Online öffnen

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 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Akzeptierte Antwort

dpb am 2 Dez. 2014

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

In MATLAB Online öffnen

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 Kommentare

7 ältere Kommentare anzeigen7 ältere Kommentare ausblenden

Ruben am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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!

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

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 am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

same holds for:

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

dpb am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

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 am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: dpb am 6 Dez. 2014

In MATLAB Online öffnen

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 am 4 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

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 am 4 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 4 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: dpb am 6 Dez. 2014

In MATLAB Online öffnen

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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Robert Cumming am 3 Dez. 2014

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

In MATLAB Online öffnen

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 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Ruben am 3 Dez. 2014

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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.

Melden Sie sich an, um zu kommentieren.

Salvador Castaneda am 23 Okt. 2021

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.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 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABGraphicsGraphics ObjectsGraphics Object Programming

Mehr zu Graphics Object Programming finden Sie in Help Center und 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!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


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

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

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

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

Europa

  • 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)

Asien-Pazifik

Kontakt zu Ihrer lokalen Niederlassung

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

References

Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6304

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.