Python, NumPy and MatPlotLib 2D Line Plot and 2D Scatter

Tutorial Video

Perquisites

We will need the numpy and matplotlib.pylot libraries:

Python

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:

Select Graphics:

Change the setting from Inline to Automatic:

Select Apply:

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:

Python

Note it is also possible to toggle between the two settings without restarting the Kernal using the following commands:

Python

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:

Python

To view the figure in some other Python IDEs, you may need to show it, this can be done using the command:

Python

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:

Python

Now that we have Figure 1000, if we once again type in:

Python

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:

Python

The command:

Python

Will close all open figures.

Data Points

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).

Python

Supposing we have recorded the speed of a rocket at the following 6 time points:

\displaystyle \begin{array}{*{20}{c}} {\text{t (s)}} & {\text{v (m/s)}} \\ 0 & 0 \\ {10} & {227.04} \\ {15} & {362.78} \\ {20} & {517.35} \\ {22.5} & {602.97} \\ {30} & {901.67} \end{array}

NumPy Vectors

We can create 2 single NumPy vectors (lists) from this data:

Python

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.

Python

NumPy Matrices

We could also create a single NumPy array with two columns.

Python

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:

Python

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:

Python

In order to plot we use these as the x and y inputs into the plot command.

Python

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

Python

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.

Python

Pandas DataFrame

Python

Recall that one can index into a dataframe using dot indexing with the column name.

Python

To perform the line plot from the data in the dataframe one can use the plot command as before:

Python

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.

Python

This last line is equivalent to:

Python

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.

Python

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
String
Full StringRGBRGBHex
rred[1,0,0][255/255,
0/255,0/255]
#ff0000
ggreen[0,1,0][0/255,255/255,0/255]#00ff00
bblue[0,0,1][0/255,0/255,0/255] #0000ff
yyellow[1,1,0][255/255,255/255,0/255] #ffff00
ccyan[0,1,1][0/255,255/255,255/255] #00ffff
mmagenta[1,0,1] [255/255,0/255,255/255] #ff00ff
kblack[0,0,0][0/255,0/255,0/255]#000000
wwhite[1,1,1][255/255,255/255,255/255]#ffffff

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 RGBHex
[192/255,0/255,0/255]#c00000
[255/255,0/255,0/255]#ff0000
[255/255,192/255,0/255]#ffbf00
[255/255,255/255,0/255]#ffff00
[146/255,208/255,80/255]#92d050
[0/255,176/255,80/255]#00b050
[0/255,176/255,240/255]#00b0f0
[0/255,112/255,96/255]#007060
[0/255,32/255,96/255]#002060
[112/255,48/255,160/255]#6f30a0
Python

Colours may also be listed as hex values.

Python

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 StyleString
None (Default)None
Solid
Dashed
Dash Dot-.
Dotted:
Python

Line Width (linewidth)

The linewidth can also be changed using the additional input argument linewidth, this is set to a numeric value.

Python

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 StringDescription
.point
,pixel
ocircle
vtriangle_down
^triangle_up
<triangle_left
>triangle_right
1tri_down
2tri_up
3tri_left
4tri_right
8octagon
ssquare
ppentagon
Pplus
*star
hhexagon1
Hhexagon2
+plus
xx
XX
Ddiamond
dthin_diamond
|vline
_hline
NoneNone
Marker Numeric
0tickleft
1tickright
2tickup
3tickdown
4caretleft
5caretright
6caretup
7caretdown
8caretleft
9caretright
10caretup
11caretdown
Python

Marker Size (markersize)

The marker size can also be modified using the additional input argument markersize, this is set to a numeric value.

Python

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.

Python

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.

Python

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.

Python

Marker Fill Style (fillstyle)

The marker fillstyle can be modified using the input argument fillstyle and setting it to a string.

Fill Style String
none
top
bottom
left
right
full
Python

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.

Python

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.

Python

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 StringLocation Integer
best0
upper right1
upper left2
lower left3
lower right4
right5
center left6
center right7
lower center8
upper center9

Opposed to using the shape of the number square which would have made much more sense.

Python

Multiple Lines

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.

Python

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.

Python

Limits

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.

Python

Scales

The functions xscale and yscale can be used to convert an axis from linear (default) to logarithmic using the strings linear and log respectively:

Python

Grid Lines

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.

axiswhich
xmajor
yminor
bothboth

For example:

Python

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

Python

We can now repeat the code with the y axis or alternatively in this case, alter the code to set the axis to both:

Python

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.

Python

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:

Python

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:

Python

Now let’s try a vector. In this case let’s relate the size of the points to the data v, because v[0]=0, we will assign an offset of 5 so we can see v[0]

Python

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.

Python

The plot label, legend, axes labels, title, limits, scale and gridlines work identically to a 2D line plot.

2D Scatter Plot Code

Python
Advertisements