bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Data Science and Scientific Python
Python•Data Science and Scientific Python

Matplotlib Scatter

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Matplotlib Scatter?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ matplotlib.pyplot as plt
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Available ColorMaps
How to Use the ColorMap
Creating Scatter Plots

Creating Scatter Plots

With Pyplot, you can use the scatter() function to draw a scatter plot.

The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()

The observation in the example above is the result of 13 cars passing by.

The X-axis shows how old the car is.

The Y-axis shows the speed of the car when it passes.

Are there any relationships between the observations?

It seems that the newer the car, the faster it drives, but that could be a coincidence, after all we only registered 13 cars.

Compare Plots

In the example above, there seems to be a relationship between speed and age, but what if we plot the observations from another day as well? Will the scatter plot tell us something else?

Example

import matplotlib.pyplot as plt
import numpy as np
#day one, the age
and speed of 13 cars:
  x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

  y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
  plt.scatter(x,
  y)
  #day two, the age and speed of 15 cars:
    x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
    y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])

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

Note

The two plots are plotted with two different colors, by default blue and orange, you will learn how to change colors later in this chapter.

By comparing the two plots, I think it is safe to say that they both gives us the same conclusion: the newer the car, the faster it drives.

Colors

You can set your own color for each scatter plot with the color or the c argument:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x,
y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])

plt.scatter(x, y, color = '#88c999')
plt.show()

Color Each Dot

You can even set a specific color for each dot by using an array of colors as value for the c argument:

Note

You cannot use the color argument for this, only the c argument.

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array(["red","green","blue","yellow","pink","black","orange","purple","beige","brown","gray","cyan","magenta"])

plt.scatter(x, y, c=colors)
plt.show()

ColorMap

The Matplotlib module has a number of available colormaps.

A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.

Here is an example of a colormap:

This colormap is called 'viridis' and as you can see it ranges from 0, which is a purple color, up to 100, which is a yellow color.

How to Use the ColorMap

You can specify the colormap with the keyword argument cmap with the value of the colormap, in this case 'viridis' which is one of the built-in colormaps available in Matplotlib.

In addition you have to create an array with values (from 0 to 100), one value for each point in the scatter plot:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')
plt.show()

You can include the colormap in the drawing by including the plt.colorbar() statement:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,
10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.show()

Available ColorMaps

You can choose any of the built-in colormaps:

NameReverse
AccentAccent_r
BluesBlues_r
BrBGBrBG_r
BuGnBuGn_r
BuPuBuPu_r
CMRmapCMRmap_r
Dark2Dark2_r
GnBuGnBu_r
GreensGreens_r
GreysGreys_r
OrRdOrRd_r
OrangesOranges_r
PRGnPRGn_r
PairedPaired_r
Pastel1Pastel1_r
Pastel2Pastel2_r
PiYGPiYG_r
PuBuPuBu_r
PuBuGnPuBuGn_r
PuOrPuOr_r
PuRdPuRd_r
PurplesPurples_r
RdBuRdBu_r
RdGyRdGy_r
RdPuRdPu_r
RdYlBuRdYlBu_r
RdYlGnRdYlGn_r
RedsReds_r
Set1Set1_r
Set2Set2_r
Set3Set3_r
SpectralSpectral_r
WistiaWistia_r
YlGnYlGn_r
YlGnBuYlGnBu_r
YlOrBrYlOrBr_r
YlOrRdYlOrRd_r
afmhotafmhot_r
autumnautumn_r
binarybinary_r
bonebone_r
brgbrg_r
bwrbwr_r
cividiscividis_r
coolcool_r
coolwarmcoolwarm_r
coppercopper_r
cubehelixcubehelix_r
flagflag_r
gist_earthgist_earth_r
gist_graygist_gray_r
gist_heatgist_heat_r
gist_ncargist_ncar_r
gist_rainbowgist_rainbow_r
gist_sterngist_stern_r
gist_yarggist_yarg_r
gnuplotgnuplot_r
gnuplot2gnuplot2_r
graygray_r
hothot_r
hsvhsv_r
infernoinferno_r
jetjet_r
magmamagma_r
nipy_spectralnipy_spectral_r
oceanocean_r
pinkpink_r
plasmaplasma_r
prismprism_r
rainbowrainbow_r
seismicseismic_r
springspring_r
summersummer_r
tab10tab10_r
tab20tab20_r
tab20btab20b_r
tab20ctab20c_r
terrainterrain_r
twilighttwilight_r
twilight_shiftedtwilight_shifted_r
viridisviridis_r
winterwinter_r

Size

You can change the size of the dots with the s argument.

Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes)
plt.show()

Alpha

You can adjust the transparency of the dots with the alpha argument.

Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis:

Example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])

y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes =
np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x,
y, s=sizes, alpha=0.5)
plt.show()

Combine Color Size and Alpha

You can combine a colormap with different sizes of the dots. This is best visualized if the dots are transparent:

Example

import matplotlib.pyplot as plt
import numpy as np
x =
np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))

colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100,
size=(100))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()
plt.show()

Previous

Machine Learning - Polynomial Regression

Next

Machine Learning - Multiple Regression