Table of contents
- Tutorial Video
- Configuring the Layout of Figures
- Data Points
- Line Colour (color – US Spelling)
- Line Style (linestyle)
- Line Width (linewidth)
- Line Marker (marker)
- Marker Size (markersize)
- Marker Edge Width (markeredgewidth)
- Marker Edge Colour (markeredgecolor – US Spelling)
- Marker Face Colour (markerfacecolor – US Spelling)
- Marker Fill Style (fillstyle)
- Marker Face Colour Alternative (markerfacecoloralt – US Spelling)
- Legend and Plot Labels
- Multiple Lines
- Labels (xlabel, ylabel, title)
- Grid Lines
- 2D Line Plot Code
- 2D Scatter Plot
- 2D Scatter Plot Code
We will need the numpy and matplotlib.pylot libraries:
Configuring the Layout of Figures
Before creating any figures, you should adjust your preferences for how you wish to display figures. The default option is inline which means all figures will be printed to the Console as shown:
If instead you want the Figures to be shown as a separate Window, you can change the setting to Automatic. To do this go to Tools → Preferences:
To do this go to Tools → Preferences:
Next on the left hand menu select iPython console:
Change the setting from Inline to Automatic:
Now go to Consoles and Restart the Kernal:
When rerunning your code, your figure will be in a separate window opposed to being inline within the Console:
Note Spyder Version 3.3 may give a stream of errors instead of making a plot. If you have this version (installed by default with the Anaconda March 2019 installer) you should close down Spyder and then update both Anaconda and Spyder. To do this open the Anaconda PowerShell Prompt and type in:
Note it is also possible to toggle between the two settings without restarting the Kernal using the following commands:
In these guides, the setting automatic will be applied and the figures will all be shown as separate windows.
To create a new figure we can use the following function. Leaving the input argument empty will create a new figure:
To view the figure in some other Python IDEs, you may need to show it, this can be done using the command:
This is not needed when Spyder is used with an Automatic Backline for Interactive figures.
If no figures are open this will be "Figure 1". We can also specify the figure number using:
Now that we have Figure 1000, if we once again type in:
We will get Figure 1000 +1 i.e. Figure 1001
The figures can be closes using the x on the top right corner or by using the command close with the input argument being the figure number in our case 1, 1000 and 1001:
Will close all open figures.
The first two inputs into the plot command used for making 2D line plots are the xdata and ydata respectively. The plot command has to be called from plt (MatPlotLib.pyplot).
Supposing we have recorded the speed of a rocket at the following 6 time points:
We can create 2 single NumPy vectors (lists) from this data:
We can plot this using the variable names of both vectors. The first vector will be the x data on the figure and the second vector will be the y data on the figure.
We could also create a single NumPy array with two columns.
To plot from this we will need to index into this using the Row and Column Indexes. Recall that we use square brackets to index into a NumPy array. We also use zero order indexing so start at 0 and go up in steps of 1. When indexing the rows are selected before the columns using a comma as a delimiter. And finally in order to select all elements of a row or column we use the semicolon :
We can open tv in variable explorer and see that for the x data, the rows we want are all of them and the column we want is 0:
We can open tv in variable explorer and see that for the y data, the rows we want are all of them and the column we want is 1:
In order to plot we use these as the x and y inputs into the plot command.
Recall that we use zero order indexing. In some cases we may not want to plot all the data. Say we want to plot all the data in all rows except for the last
Recall that we can set the lower bound before the colon and the upper bound after the colon. Recall with zero order indexing we go up to the last value but never reach it stopping at the value before. So this will plot row0, row1, row2, row3 and row4. Compare the differences in the axes on Figure 2 and Figure 3 and you'll notice that the data on the last row, row5 is missing.
For practice with indexing try to plot the data from row1, row2 and row3 only. Try to plot all the data using a colon with an upper and lower bound. Repeat for the individual vectors t and v.
Recall that one can index into a dataframe using dot indexing with the column name.
To perform the line plot from the data in the dataframe one can use the plot command as before:
It is also possible to index a selection using dot indexing with the row names or using the row numbers for instance the column t is indexed using the row names and the column v is indexed using the row numbers.
This last line is equivalent to:
Note the additional set of square brackets used with indexing a list of specified points opposed to indexing a using a colon or colon with upper and lower bound (this also applies when referencing data within a NumPy matrix). It is advisable for the reader to create their own dataset with four columns and practice that they can plot each column on a separate figure and that they can plot a selection of data using a colon, a colon with an upper and lower bound, a selection using the row names and a selection using the row numbers to ensure they are up to scratch with indexing.
Line Colour (color – US Spelling)
For convenience we will return to plotting data using two single vectors and we will now look at changing properties of the plot using additional input arguments. Let's start with the colour of the line. Note Python uses the US spelling of colour which does not have the u, color.
Primary and Secondary colours aswell as Black and White can be encoded using a single letter string and also a string of the full name of the colour.
|Single Letter |
For more fine tuning colours can be specified as a vector of [r,g,b] values. Many programs list this vector of [r,g,b] values between 0 and 255 but Python recognises these are normalised values between 0 and 1. For instance the standard colours in Microsoft Word are as follows. Using these colours may be useful if you want to keep consistency with plots and a Word Document for instance.
|Microsoft Word RGB||Hex|
Colours may also be listed as hex values.
For more details about the colour scheme see:
In general one uses a lookup table to select the [R,G,B] or Hex value desired however it is useful to keep a list together of your favourite colours.
Line Style (linestyle)
The line style can be selected using the additional input argument linestyle. linestyle can have the following string arguments.
Line Width (linewidth)
The linewidth can also be changed using the additional input argument linewidth, this is set to a numeric value.
Line Marker (marker)
It is possible to add a Marker at every data point using the argument marker and setting it to a string or number.
Marker Size (markersize)
The marker size can also be modified using the additional input argument markersize, this is set to a numeric value.
Marker Edge Width (markeredgewidth)
The marker edge width (thickness of the marker outside line) can also be modified using the additional input argument markeredgewidth, this is set to a numeric value.
Marker Edge Colour (markeredgecolor – US Spelling)
The marker edge colour can be modified using the input argument markeredgecolor and setting it to a colour string, [R,G,B,] or hex value as discussed when looking at the input argument color.
Marker Face Colour (markerfacecolor – US Spelling)
The marker face colour can be modified using the input argument markerfacecolor and setting it to a colour string, [R,G,B,] or hex value as discussed when looking at the input argument color.
Marker Fill Style (fillstyle)
The marker fillstyle can be modified using the input argument fillstyle and setting it to a string.
|Fill Style String|
Marker Face Colour Alternative (markerfacecoloralt – US Spelling)
If the fillstyle is set to a direction (top, bottom, left, right), then the marker face has both a colour and alternative colour. This can be modified using the input argument markerfacecoloralt and setting it to a colour string, [R,G,B,] or hex value as discussed when looking at the input argument color.
Legend and Plot Labels
The plot can be assigned a label set as a string. This will show up on the chart if the command legend() is typed.
The location of the legend can be set using the input argument loc and assigning it to a string or a number. Note once again that English US is used for center opposed to the English UK version centre. Unfortunately with the numerical input, it is implemented in the following way:
|Location String||Location Integer|
Opposed to using the shape of the number square which would have made much more sense.
Unless a new figure is selected or the figure is closed, then additional plots will be added on top of the existing plot, this will allow multiple lines to be plotted.
Labels (xlabel, ylabel, title)
The axis labels and figure title can be input using the commands xlabel, ylabel and title with input arguments as strings.
It is possible to adjust the limits of the xaxis and yaxis using the function xlim and ylim. These functions have 2 value vectors as input arguments.
The functions xscale and yscale can be used to convert an axis from linear (default) to logarithmic using the strings linear and log respectively:
Gridlines may also be selected usign the function grid. This like the line plot has multiple input arguments such as color, linestyle and linewidth which have all been seen before. There are two additional input arguments, axis and which, which can have the following string input arguments.
By default the minor gridlines are not enabled. These need to be enabled using the function minor_ticks_on which should be called with an empty argument or True
We can now repeat the code with the y axis or alternatively in this case, alter the code to set the axis to both:
2D Line Plot Code
It is worthwhile putting all the code used to create the plot above together. The user is advised to modify it and use it with some of their own test data.
2D Scatter Plot
The 2D scatter plot is very similar to the 2D line plot. However the former places an emphasis on the line and the individual datapoints denoted by markers are secondary in nature, the latter places an emphasis solely on the individual datapoints.
Like the line plot, the first two input arguments are the x-data and the y-data:
The scatter plot has a third input argument which selects the marker size. This can be a scalar where all markers will be the same size or it can be a vector of the same length as the x and y data. First let's use a scalar:
Now let's try a vector. In this case let's relate the size of the points to the data v, because v=0, we will assign an offset of 5 so we can see v
Many of the additional input arguments have the same name however, many of the properties such as color, linewidth correspond directly to the marker. The input argument color is used alone and applies to a filled marker. It is also possible to specify an edgecolor and facecolor separately, note how marker is docked from these in the scatter plot as all the defaults apply to the marker.
The plot label, legend, axes labels, title, limits, scale and gridlines work identically to a 2D line plot.