This is extremely useful but one should be careful that they don't overwrite a figure by accident when using it.
Data
We need some data to plot. We can create this data in Octave/MATLAB or input it from a spreadsheet in the likes of Excel. We are going to copy data from excel using the variable editor (the copy paste method described here will only work for data that contains numbers, if an excel cell that is copied contains a letter it will not work). In MATLAB we can directly right click the variable editor and create a new variable. In Octave we need to first make a variable and assign it to a scalar before it allows to copy and paste excel data into the variable editor. So for instance using variable names which match the tab in the excel spreadsheet we can type in the following gives a rawdata a scalar of 1:
rawdata=1
We can double click
rawdata
in the workspace to open up in the variable editor. Here we see a spreadsheet with a single scalar value of 1 as expected. Now we can right click and select paste table which will paste the Excel data:
The excel data is pasted across:
We can then select the other tab of the excel data sheet interdata which is interpolated data calculated from the rawdata. First we will need to create the new variable and assign in to a scalar
interdata=1
Then we can open up interdata in the variable editor by double clicking it in the workspace. You may want to press the x to close rawdata in the variable editor (it will remain in the workspace) to give the most screen space:
Once again we can copy and paste across from excel by right clicking and using Paste Table:
The data we want to plot is now in Octave:
The data I am going to use can also be generated using the script here:
[code language="matlab"]
% Data
rawdata=[0,0;
10,227.04;
15,362.78;
20,517.35;
22.5,602.97;
30,901.67];
% Lower and Upper Limits for interpolation
m=1;
n=30;
More details about it are available in my guide on Interpolation.
Column Selection
In order to have data to plot, we need to be able to select columns from the variable. I go in quite some details about how to do this in my guide Introduction to Arrays however here what we need to know is that for a variable called
variable
, in order to select the first column we need to type in
The figure here is very basic. Notice how the blue line is narrow and the text is quite tiny. There is also no axis label or legend. All of these can be tweaked with additional lines of code. Let us open up the Editor tab and create a new script and paste the last line into it:
We can then press the save button, once we do this a dialogue box shows and we can input a script name and select save. More details about variable names are in my guide Introduction to Arrays.
Line Width
Let's us first thicken the line so we can see it in more detail, to do this we need to add the additional input argument(s)
this is because LineWidth unenclosed would be taken by Octave/MATLAB as a variable or function. Instead we want this input argument to be a line of text, known as a string. These input arguments are matched so we must immediately specify the line width we wish to use, in this case we are using the scalar 5. In general these functions are setup so the case of the string doesn't matter
Note: The colours shown in the script editor are different to the ones I am using in these notes, because I am using colours to highlight some things.
When editing code in the script editor, be sure to save it before you run it by pressing the save button. Unsaved code will have a * at the beginning of the title as shown. Once saved right click the script and select run:
Now the Line Width is updated to a thickness of 5:
Font Size
Another thing that is hard to see is the text. To update the size of this we need to use the function
Here we need to be careful because Octave/MATLAB use American English and not British English, so we need to spell colour as color. This input argument needs to be followed by the colour. As you can see the line is now red opposed to blue.
MATLAB/Octave have the following strings for the primary and secondary colours:
Black is shown in the absence of these three colours and white is shown when these three colours are at full brightness.
Many programs list colors in 8 bit, that means they list levels. If we subtract 1 from 256 to get a 0 value with range from 0-255. You may use colour pickers in these programs to determine the colour you want to use. Let's use the colour picker in Microsoft Word for example. Select the button to change the colour of the text, select More Colours:
Select your colour and then select custom:
You now get your RGB values listed from 0 to 255. We can normalise these to 255 but dividing them through by 255 when using them in Octave/MATLAB:
row vector values and can ignore the Hue, Sat and Lum:
Line Style
Octave/MATLAB also has a number of different types of lines, by default the line is a solid line however another two input arguments can be input to change these:
To see this in the script editor I have split the code on separate lines by pressing enter after the commas, input arguments are shown in pairs. The code will still run like this.
Marker Size
The markers above were quite small, they are hard to read, this can be resolved by using another two additional input arguments:
The Marker does not need to be the same colour as the line. We can specify both the Edge and the Fill of the marker with another colour using another four input arguments. Once again
and has some slight advantages but has slightly different input arguments, I'm not looking to cover it here as one can also make a scatter plot by using the short hand notation for plot, the long hand notation does not work because it will default to plot a line if the input argument
standing for get current axis meaning you are changing the properties of the axis. To change the axis limits we use the same function with different input arguments or alternatively we can combine the input arguments and put them on one line.
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed of a rocket with respect to time</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
Quite often one may have created a cell array with the labels, these can also be input as axis labels. To create a cell array, one needs to use curly brackets. We can create for instance
<span style="color: #800080;"><span style="color: #ff0000;"><span style="color: #000000;">mytestlabels=</span>{</span>'<span style="color: #ff9900;">test</span> </span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'<span style="color: #000000;">,</span>'<span style="color: #ff9900;">test</span> <span style="color: #ff9900;">speed (m/s)</span>'<span style="color: #000000;">,'<span style="color: #ff9900;">test</span> <span style="color: #ff9900;">speed of a rocket with respect to time</span>'<span style="color: #ff0000;">}</span></span></span>
For the location we use the compass points, these are usually within the chart area but we can also specify for them to be outside. Best is leaving MATLAB/Octave to find the best location:
For this next part, I want the raw data to be plotted using red dots and no line.
[code language="matlab"]
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;pre&amp;amp;amp;gt;[/code]
To this plot, we want to add a straight dotted line of the nearest point interpolated data from the variable
Octave/MATLAB will create a new figure, in the case the lowest unused figure is Figure 2. To Figure 2, Octave plotted the rawdata with red markers but then it seen a new plot command and replotted the dataint as a blue dotted line removing the former plot of raw data with red markers.
command should be used when finished adding all the data you want to the current figure and then if you want to create a separate figure you should use the command
<span style="color: #3366ff;">figure</span>
afterwards.
Closing figure 1 and figure 2 and relaunching the updated script:
Now we can see how the nearest interpolation looks with respect to the original data. Since the plot now has two items, a combined legend can be made:
[code language="matlab"]
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
[/code]
In separate figures I also want the linear interpolated data and cubic interpolated data. These are the 3rd and 4th column of dataint respectively. To do this I can copy and paste the above code 3 times and update line 17 to use the 3rd and 4th column instead of the second and line 20 to update the legend labels accordingly.
[code language="matlab"]
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,3),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','linear interpolation','Location','NorthWest')
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,4),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','cubic interpolation','Location','NorthWest')
[/code]
Subplots
We have three separate figures, it is possible to combine them together using a subplot. Subplotting essentially splits the figure window up into a m by n matrix
Not all subplots need to be the same size, the third element can be set as a row vector. In the case of wanting 3 small plots at the top and one large plot at the bottom the following can be used
I can change my code to plot the three charts in a 1 by 3 subplot by editing lines 2, 22 and 42 to add in subplot and to remove figure which was at lines 22 and 42.
[code language="matlab"]
figure
subplot(1,3,1)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
subplot(1,3,2)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,3),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','linear interpolation','Location','NorthWest')
subplot(1,3,3)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,4),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','cubic interpolation','Location','NorthWest')
[/code]
In this case the legend's don't look so great so I can change their Location from
. To do this quickly I can use find and replace by pressing [Ctrl] and [ f ]
Octave struggles a little as the font size I am using is much larger than the default. :
Custom Plot Function
As you seen, by default Octave plots a line like:
Which isn't very aesthetically pleasing. Say we prefer plotting a red dotted line of Line Width 5, have axes of Font Size 20 and have both the major and minor grid lines on, we can create a function to do so. To create a function:
You will need to specify a name for the function. Function names follow the same rules as the rules for variables or for scripts, see my guide assigning variable names for more details.
Functions have their own internal workspace which is separate from the main workspace. The only way to get variables into the functions internal workspace from the workspace is to use inputs and the only way to get variables back into the workspace from the functions internal workspace is to use outputs.
Since we are looking to create a plot and not wanting any output variables in the main workspace we can remove the output variable thus:
% This will plot a dotted red line of thickness 5.
% The graph will have gridlines and the font size will be 20
% x and y labels are applied
% save as plot1
function plot1 (x, y)
plot(x,y,'r:','LineWidth',5);
set(gca, 'FontSize',20);
grid on;
grid minor;
xlabel('x')
ylabel('y')
end
[/code]