Python matplotlib – Format scatter plot

In this post we will be creating scatter plots via Python and formatting the colours on the graph, to do this will be using the matplotlib library.

The script will make use of two data arrays of equal size, one of which will be applied to our X axis and the other our Y axis. A sample of out data arrays can be seen below, both of which contain ten data points.

xAxis = [1,2,3,4,5,6,7,8,9,10]
yAxis = [55,66,69,72,85,92,101,95,82,75]

We start by creating a scatter graph using the snippet of code below.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]
y = [55,66,69,72,85,92,101,116,102,95]

plt.plot(x, y)
plt.show()

By default, this will return a blue line chart as seen below. In single character shorthand notation the format is seen as ‘b-‘, where ‘b’ represents the colour blue and ‘-‘ represents a line chart.

Scatter plot with blue line

These single character shorthand notations can be changed, the options for which can be found below.

Colour notations

  • 'b' as blue
  • 'g' as green
  • 'r' as red
  • 'c' as cyan
  • 'm' as magenta
  • 'y' as yellow
  • 'k' as black
  • 'w' as white

Marker notations

  • “.” as point
  • “–” as dashes
  • “o” as circle
  • “v” as triangle down
  • “^” as triangle up
  • “s” as square
  • “x” as X
  • “*” as star

And so if we are wanting yellow star symbols displayed on our scatter plot instead, we would use “y*”, the code for which can be found below.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]
y = [55,66,69,72,85,92,101,116,102,95]

plt.plot(x, y, "y*") #Shorthand notations added to the end here
plt.show()

The above snippet of code would return the following as output.

Scatter plot with yellow star symbols

Leave a Reply