# Difference between revisions of "Recap of the hierarchy of each plot element"

m (Use developer's coding habits) |
|||

Line 1: | Line 1: | ||

− | + | Octave aims at being compatible with Matlab as much as possible, so the graphics part is very similar too to Matlab. In Octave the first choice to make is the {{Codeline|graphics_toolkit()}}. Standard is the 'gnuplot' toolkit using the [http://www.gnuplot.info Gnuplot] software package . The second choice is [http://www.fltk.org 'fltk']. You might want to try to test both of them for your plotting aims to see which solves your problem. | |

After the choice of the {{Codeline|graphics_toolkit('gnuplot')}} or {{Codeline|graphics_toolkit('fltk')}}, there is the following hierarchy to address when make/adapting your plot: | After the choice of the {{Codeline|graphics_toolkit('gnuplot')}} or {{Codeline|graphics_toolkit('fltk')}}, there is the following hierarchy to address when make/adapting your plot: | ||

* root (any hierarchy needs to start somewhere) | * root (any hierarchy needs to start somewhere) | ||

− | * gcf() (the handle to your figure: one for every figure) | + | * {{Codeline|gcf ()}} (the handle to your figure: one for every figure) |

− | * gca() (the handle to the axes inside a particular figure (several if you have subplots) | + | * {{Codeline|gca ()}} (the handle to the axes inside a particular figure (several if you have subplots) |

− | * p=plot(x,y) (p is the handle (inside the current axes handle) to the data, data symbols, data line thickness, etc) | + | * {{Codeline|<nowiki>p = plot (x, y)</nowiki>}} (p is the handle (inside the current axes handle) to the data, data symbols, data line thickness, etc) |

− | * t=text(xlocation,ylocation, | + | * {{Codeline|<nowiki>t = text (xlocation, ylocation, "some text")</nowiki>}} (t is the handle to the text and is a child of the current axes handle, like p) |

− | * pp=patch([x1 x2 x3 x4], [y1 y2 y3 y4], | + | * {{Codeline|<nowiki>pp = patch ([x1 x2 x3 x4], [y1 y2 y3 y4], "r")</nowiki>}} (pp is the handle to a red patch using the coordinates inside the current axes handle) |

Let's do an example: | Let's do an example: | ||

− | graphics_toolkit( | + | graphics_toolkit ("gnuplot"); |

− | x=0:0.1:3; | + | x = 0:0.1:3; |

− | y=sin(x); | + | y = sin (x); |

− | p=plot(x,y, | + | p = plot (x, y, "b"); |

This should get you a plot of a part of a sine wave. Octave has used all standard properties like line widths, fonts, etc, except for the line color which was forced to be blue (via the {{Codeline|'b'}}). | This should get you a plot of a part of a sine wave. Octave has used all standard properties like line widths, fonts, etc, except for the line color which was forced to be blue (via the {{Codeline|'b'}}). | ||

Line 21: | Line 21: | ||

Before going into the hierarchy and how to change things, let's make things more complicated: | Before going into the hierarchy and how to change things, let's make things more complicated: | ||

− | graphics_toolkit( | + | graphics_toolkit ("gnuplot"); |

− | figure(1) | + | figure (1) |

− | x=0:0.1:3; | + | x = 0:0.1:3; |

− | y=sin(x); | + | y = sin (x); |

− | p=plot(x,y, | + | p = plot (x, y, "b"); |

− | figure(2) | + | figure (2) |

− | subplot(2,1,1); | + | subplot (2, 1, 1); |

− | r=plot(x,y.^2, | + | r = plot (x, y.^2, "og"); |

− | subplot(2,1,2); | + | subplot (2, 1, 2); |

− | q=plot(x,x.^2, | + | q = plot (x, x.^2, "k"); |

− | * You now have 2 windows that popped up on your screen: | + | * You now have 2 windows that popped up on your screen: figure 1 and figure 2. |

− | * | + | * figure 2 has two axes objects inside: a {{Codeline|<nowiki>y = sin (x) .^2</nowiki>}} and a {{Codeline|<nowiki>y = x.^2</nowiki>}}. |

* the actual data and data-plot-properties are inside the handles {{Codeline|p}} and {{Codeline|r}}. | * the actual data and data-plot-properties are inside the handles {{Codeline|p}} and {{Codeline|r}}. | ||

So let's say you want to change the line thickness of the data in the first figure: | So let's say you want to change the line thickness of the data in the first figure: | ||

− | figure(1) | + | figure (1) |

− | set(p, | + | set (p, "linewidth", 3) |

You can get the color of the two other plots by referring to their handles: | You can get the color of the two other plots by referring to their handles: | ||

− | get(q, | + | get (q, "color") |

− | get(r, | + | get (r, "color") |

Which will give you the RGB code for black (0,0,0) and green (0,1,0). | Which will give you the RGB code for black (0,0,0) and green (0,1,0). | ||

Have a look at all the things you can change with: | Have a look at all the things you can change with: | ||

− | get(q) | + | get (q) |

And {{Codeline|set}} anything that is not to your taste to something else (for what's available see the [http://www.gnu.org/software/octave/doc/interpreter/ manual]). | And {{Codeline|set}} anything that is not to your taste to something else (for what's available see the [http://www.gnu.org/software/octave/doc/interpreter/ manual]). | ||

− | set(p, | + | set (p, "marker", "*") |

Adding {{Codeline|text()}} inside an {{Codeline|axes()}} object is done by | Adding {{Codeline|text()}} inside an {{Codeline|axes()}} object is done by | ||

− | text(2,0.8, | + | text (2, 0.8, "HERE"); |

... but it now is inserted in figure1, which ''might NOT be want you anticipated''. | ... but it now is inserted in figure1, which ''might NOT be want you anticipated''. | ||

The {{Codeline|text()}} command does ''not'' have the option to tell it in which figure or axes object to write the text. | The {{Codeline|text()}} command does ''not'' have the option to tell it in which figure or axes object to write the text. | ||

Make sure you have moved to current figure and axes ''before'' calling {{Codeline|text()}} to insert text: | Make sure you have moved to current figure and axes ''before'' calling {{Codeline|text()}} to insert text: | ||

− | figure(2) % the following command will operate on figure(2) | + | figure (2) % the following command will operate on figure(2) |

− | get(gcf(), | + | get (gcf (),"children") % which two axes objects are there inside the current figure ? |

− | set(gcf(), | + | set (gcf (), "currentaxes", (get (gcf (), "children"))(2)) % chose the second set of axes |

− | text(1.0,0.5, | + | text (1.0, 0.5, "THIS IS WHAT I WANTED") |

## Revision as of 20:52, 3 August 2012

Octave aims at being compatible with Matlab as much as possible, so the graphics part is very similar too to Matlab. In Octave the first choice to make is the `graphics_toolkit()`

. Standard is the 'gnuplot' toolkit using the Gnuplot software package . The second choice is 'fltk'. You might want to try to test both of them for your plotting aims to see which solves your problem.

After the choice of the `graphics_toolkit('gnuplot')`

or `graphics_toolkit('fltk')`

, there is the following hierarchy to address when make/adapting your plot:

- root (any hierarchy needs to start somewhere)
`gcf ()`

(the handle to your figure: one for every figure)`gca ()`

(the handle to the axes inside a particular figure (several if you have subplots)`p = plot (x, y)`

(p is the handle (inside the current axes handle) to the data, data symbols, data line thickness, etc)`t = text (xlocation, ylocation, "some text")`

(t is the handle to the text and is a child of the current axes handle, like p)`pp = patch ([x1 x2 x3 x4], [y1 y2 y3 y4], "r")`

(pp is the handle to a red patch using the coordinates inside the current axes handle)

Let's do an example:

graphics_toolkit ("gnuplot"); x = 0:0.1:3; y = sin (x); p = plot (x, y, "b");

This should get you a plot of a part of a sine wave. Octave has used all standard properties like line widths, fonts, etc, except for the line color which was forced to be blue (via the `'b'`

).

Before going into the hierarchy and how to change things, let's make things more complicated:

graphics_toolkit ("gnuplot"); figure (1) x = 0:0.1:3; y = sin (x); p = plot (x, y, "b"); figure (2) subplot (2, 1, 1); r = plot (x, y.^2, "og"); subplot (2, 1, 2); q = plot (x, x.^2, "k");

- You now have 2 windows that popped up on your screen: figure 1 and figure 2.
- figure 2 has two axes objects inside: a
`y = sin (x) .^2`

and a`y = x.^2`

. - the actual data and data-plot-properties are inside the handles
`p`

and`r`

.

So let's say you want to change the line thickness of the data in the first figure:

figure (1) set (p, "linewidth", 3)

You can get the color of the two other plots by referring to their handles:

get (q, "color") get (r, "color")

Which will give you the RGB code for black (0,0,0) and green (0,1,0).

Have a look at all the things you can change with:

get (q)

And `set`

anything that is not to your taste to something else (for what's available see the manual).

set (p, "marker", "*")

Adding `text()`

inside an `axes()`

object is done by

text (2, 0.8, "HERE");

... but it now is inserted in figure1, which *might NOT be want you anticipated*.

The `text()`

command does *not* have the option to tell it in which figure or axes object to write the text.
Make sure you have moved to current figure and axes *before* calling `text()`

to insert text:

figure (2) % the following command will operate on figure(2) get (gcf (),"children") % which two axes objects are there inside the current figure ? set (gcf (), "currentaxes", (get (gcf (), "children"))(2)) % chose the second set of axes text (1.0, 0.5, "THIS IS WHAT I WANTED")