How to format plot axes with Latex in Scilab

Quite often we need to plot mathematical expressions or values, which are function of angles, which might be measured in degrees (°) or radians (π). By default, in Scilab, the axes tick labels are set in decimal values, even if the angle is given in radians.

How to install Scilab

For example we want to plot a trigonometric equation function of an angle between 0 and 4π. For this we’ll write the following Scilab script:

t = 0:0.01:4*%pi;
f = sin(t).^2+cos(t);
plot(t,f,'LineWidth',2)

where t is our angle in radians and f our trigonometric function.

By running this piece of code we get the following graphical window:

Trigonometric function plot in Scilab

Image: Trigonometric function plot in Scilab

Without any formatting the plot doesn’t look very professional. Let’s add a grid, axes labels, title and limit the horizontal axis to the maxim value of the function argument. This formatting is performed with the following Scilab instructions:

xgrid()
xlabel('t [decimal]','FontSize',2)
ylabel('f(t)','FontSize',2);
title('$f(t)=sin^2(t)+cos(t)$','FontSize',3);
ha=gca();
ha.tight_limits = ["on","on","on"]
ha.data_bounds=[min(t),min(f)-0.1;max(t),max(f)+0.1];

By running this additional piece of code our graphical window is updated accordingly:

Trigonometric function plot in Scilab - formatted

Image: Trigonometric function plot in Scilab – formatted

This is better but not complete. As you can see, the ticks on the x-axis are labeled with decimal numbers. The values correspond to the angle (e.g. 3.1416 = π) but it’s not what we expected. For a better understanding of the behaviour of the function we need to have the x-axis in radians, exactly as the function argument (variable t).

To understand how the axis tick labels are set, we’ll use the gca() function handle. For our example, the tick labels of the x-axis are displayed by entering the instruction ha.x_ticks at the Scilab console:

--> ha.x_ticks
ans =
ans(1)
!ticks locations labels !
ans(2)
0.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
ans(3)
!0 !
!1 !
!2 !
!3 !
!4 !
!5 !
!6 !
!7 !
!8 !
!9 !
!10 !
!11 !
!12 !
-->

This means that the axis tick labels are formatted as a Tlist variable, which contains two arrays: first with the locations of the ticks, second with the labels of the ticks.

To format both the locations and the labels we need to use this Scilab format:

ha.x_ticks=tlist(["ticks","locations","labels"],locations_array,labels_array);

For out particular case, we are going to setup the locations in decimal equivalent of the radians, starting with 0 and incrementing with π/2 up to . The labels are going to be setup with the same values but in Latex string format, as:

ha.x_ticks=tlist(["ticks","locations","labels"],..
   [0;%pi/2;%pi;3*%pi/2;2*%pi;5*%pi/2;3*%pi;7*%pi/2;4*%pi],..
   ["$\Large{0}$";..
   "$\Large{\frac{\pi}{2}}$";..
   "$\Large{\pi}$";..
   "$\Large{\frac{3\pi}{2}}$";..
   "$\Large{2\pi}$";..
   "$\Large{\frac{5\pi}{2}}$";..
   "$\Large{3\pi}$";..
   "$\Large{\frac{7\pi}{2}}$";..
   "$\Large{4\pi}$"]);
   
xlabel('t [rad]','FontSize',2)

The size of both arrays must match, otherwise the formatting by not be applied. In our example we used double dots .. to be able to continue the instruction on the following row. Running the above Scilab instructions will output the following graphical window:

Trigonometric function plot in Scilab - Latex axes tick labels

Image: Trigonometric function plot in Scilab – Latex axes tick labels

As you can see the vertical grid is now setup function of the position of the x-axis ticks. Also, the labels of the ticks are now represented in radians using Latex format. The same formatting method can be applied as well to the y-axis.

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

Don’t forget to Like, Share and Subscribe!

One Response

Leave a 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