Advanced Scilab plot formatting using gcf() and gca() functions

In Scilab, the graphical representations can be formatted in two ways:

  • by using Scilab instructions and functions executed in a script or at the Scilab console
  • by using the Figure and Axes Properties from the menu bar

First, let’s plot a simple sine function with the following Scilab instructions:

x = 0:0.2:2*%pi;
y = sin(x);
plot(x,y);
xlabel('x [rad]','FontSize',3);
ylabel('sin(x)','FontSize',3);
title('y(x) = sin(x)','FontSize',3);
xgrid();

Running the Scilab instruction above will generate the following graphical window:

Scilab plot of a sine wave

Image: Scilab plot of a sine wave

Some of the graph properties are already setup, using the FontSize property for x, y labels and title. The grid is also displayed thanks to xgrid() function. There are a lot more properties to set, for figure, axes and line. We can acces all the properties of the graphical window by clicking on Edit ->  Figure Properties.

The Figure Editor window will pop-up:

Scilab Figure Editor

Image: Scilab Figure Editor

The properties of the graphical window are displayed hierarchically, in the Object Browser. The parent is the Figure graphic handle. It has three Object Properties: Style, Mode and Colormap. By clicking on each of the object properties, we have access to all the parameters of the figure.

The Figure graphical handle has children, the Axes. By clicking on it a new window will pop-up with all the properties related to the axes. In the same time, the Axes graphical handle has children, the Compound which has children the  Polyline. This latest contains all the graphical properties related to the line plot.

We can format our graphical window easily with the Figure Editor because we have access to all the parameters. This method is not very efficient since all settings have to be repeated for every new graphical window.

A more convenient way is to store all the graphics handle for both Figure and Axes in Scilab variables. To do this, we need to run the get current figure function gcf() and get current axes function gca() and store the parameters in the graphical handle variables hf and ha.

hf = gcf();
ha = gca();

First let’s have a look to what figure graphical properties we have access. To do this just display the hf variable in the Scilab console. Be aware that the graphic handle properties are available as long as the graphical window is open. Once you close it, the graphic handle variables are no longer available in the Scilab console.

-->hf
hf =

Handle of type "Figure" with properties:
========================================
children: "Axes"
figure_position = [1818,394]
figure_size = [610,552]
axes_size = [610,460]
auto_resize = "on"
viewport = [0,0]
figure_name = "Graphic window number %d"
figure_id = 0
info_message = ""
color_map = matrix 32x3
pixel_drawing_mode = "copy"
anti_aliasing = "off"
immediate_drawing = "on"
background = -2
visible = "on"
rotation_style = "unary"
event_handler = ""
event_handler_enable = "off"
user_data = []
resizefcn = ""
closerequestfcn = ""
resize = "on"
toolbar = "figure"
toolbar_visible = "on"
menubar = "figure"
menubar_visible = "on"
infobar_visible = "on"
dockable = "on"
layout = "none"
layout_options = "OptNoLayout"
default_axes = "on"
icon = ""
tag = ""

-->

First thing to notice is the children property. As expected it is Axes, the same we’ve seen in the Figure Editor above. All the figure properties can be read and modified. The graphical handle variables hf and ha are structures so they can be accessed using the . operator:

-->hf.toolbar_visible
ans =

on

-->

The first change we are going to do is to hide the toolbar, menubar and infobar. To do this we’ll call the graphical property and assign the desired value. Make sure you enter the same data type (string, scalar, array, etc.) when setting properties.

hf.toolbar_visible = "off";
hf.menubar_visible = "off";
hf.infobar_visible = "off";

Now, let’s change the size of the graphic window. Make sure you enter the window size as an array.

hf.figure_size = [640 480];

The last change we are going to do on the figure handle is to set a figure name.

hf.figure_name = 'Plot of sin(x) function';

The next changes are going to be performed on the axis handle. We can access the axes properties in two ways:

  • from figure handle variable, as a structure: hf.children
  • directly from the ha graphic handle variable

For our example we will use the ha variable. To display the axes properties enter the ha variable at the Scilab console:

-->ha
ha =

Handle of type "Axes" with properties:
======================================
parent: Figure
children: "Compound"

visible = "on"
axes_visible = ["on","on","on"]
axes_reverse = ["off","off","off"]
grid = [0,0]
grid_position = "background"
grid_thickness = [1,1]
grid_style = [3,3]
x_location = "bottom"
y_location = "left"
title: "Label"
x_label: "Label"
y_label: "Label"
z_label: "Label"
auto_ticks = ["on","on","on"]
x_ticks.locations = matrix 15x1
y_ticks.locations = matrix 11x1
z_ticks.locations = []
x_ticks.labels = matrix 15x1
y_ticks.labels = matrix 11x1
z_ticks.labels = []
ticks_format = ["","",""]
ticks_st = [1,1,1;0,0,0]
box = "on"
filled = "on"
sub_ticks = [0,1]
font_style = 6
font_size = 1
font_color = -1
fractional_font = "off"

isoview = "off"
cube_scaling = "off"
view = "2d"
rotation_angles = [0,270]
log_flags = "nnn"
tight_limits = ["off","off","off"]
data_bounds = [0,-1.00;6.2,1]
zoom_box = []
margins = [0.12,0.12,0.12,0.12]
auto_margins = "on"
axes_bounds = [0,0,1,1]

auto_clear = "off"
auto_scale = "on"

hidden_axis_color = 4
hiddencolor = 4
line_mode = "on"
line_style = 1
thickness = 1
mark_mode = "off"
mark_style = 0
mark_size_unit = "tabulated"
mark_size = 0
mark_foreground = -1
mark_background = -2
foreground = -1
background = -2
arc_drawing_method = "lines"
clip_state = "clipgrf"
clip_box = []
user_data = []
tag =

-->

As you can see, there are a lot more properties to set at the axes level. Also the children of the Axes handle is the Compound.

For our example we are going to change the grid style, grid color, font size and font color for the axes:

ha.grid_style = [7,7];
ha.grid = [29,29];
ha.font_size = 2;
ha.font_color = 2;

The last properties to modify are the line properties. To access them we need to use the structure data of the ha graphic handle variable.

-->ha.children
ans =

Handle of type "Compound" with properties:
==========================================
parent: Axes
children: "Polyline"
visible = "on"
user_data = []
tag =

-->

Entering ha.children at the Scilab console will output the Compound property. To access the Polyline, we have to go one level deeper.

-->ha.children.children
ans =

Handle of type "Polyline" with properties:
==========================================
parent: Compound
children: []
datatips: []
display_function: ""
display_function_data: []
visible = "on"
data = matrix 32x2
closed = "off"
line_mode = "on"
fill_mode = "off"
line_style = 1
thickness = 1
arrow_size_factor = 1
polyline_style = 1
foreground = 2
background = -2
interp_color_vector = []
interp_color_mode = "off"
mark_mode = "off"
mark_style = 0
mark_size_unit = "point"
mark_size = 0
mark_foreground = -1
mark_background = -2
mark_offset = 0
mark_stride = 1
x_shift = []
y_shift = []
z_shift = []
bar_width = 0
clip_state = "clipgrf"
clip_box = []
user_data = []
tag =

-->

Now we have access to all the graphical properties of the line plot.

For our example we are going to change the thickness of the line, add marks on the line and change the mark style, sizeforeground and background colors.

ha.children.children.thickness = 1;
ha.children.children.mark_mode = "on";
ha.children.children.mark_style = 5;
ha.children.children.mark_size = 8;
ha.children.children.mark_foreground = 2;
ha.children.children.mark_background = 5;

After performing all the above changes, we will end up with the following graphical window.

Scilab plot formatted with gcf() and gca()

Image: Scilab plot formatted with gcf() and gca()

Formatting our graphical window with the figure and axes handle is a very powerful technique. It gives to the user the possibility to preformat the graphical window and use it as a template for future plots. Also, through the handle variable, all the properties of the graphical window can be accessed and modified.

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

2 Comments

  1. Denis
  2. Foad

Leave a Reply to Denis Cancel reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh