How to draw scatter plot using Matplotlib

Scatter plot drawn using Matplotlib
Scatter plot drawn using Matplotlib

Scatter plots are useful to show data points that lie in 2D. Drawing a scatter plot in Matplotlib is easy using the scatter function.

  • Assuming your data points are available as two NumPy arrays of shape (N, 1), drawing a scatter plot is straightforward:
import matplotlib.pyplot as mplot

# x_vals is NumPy array of shape (N, 1)
# y_vals is NumPy array of shape (N, 1)
mplot.scatter(x_vals, y_vals)
  • By default, markers that are filled discs, that is of type o, are drawn. This can changed to any of the other markers available in Matplotlib using the marker input argument. The different markers of Matplotlib are shown here.

  • The size of the marker is 20 by default. It can be changed by setting the s input argument.

  • The edge or border of the markers are drawn in black by default. If you do not want the edges to be drawn, then set the edgecolors input argument to none (a string, not the None value).

  • The color that is filled inside the marker is called the face color. It can be set by using the facecolors input argument. For setting the RGB values of N points, pass a NumPy array of shape (N, 3) where each color value lies in (0, 1).

  • If a dense set of data points is being drawn, multiple markers could obscure each other. This situation can be improved by adding some transparency to the markers. This can be set using the alpha input argument.

  • An example that uses all the above customizations to draw the figure shown above:

import matplotlib.pyplot as mplot

# x_vals is NumPy array of shape (N, 1)
# y_vals is NumPy array of shape (N, 1)
# c_arr  is NumPy array of shape (N, 3)

mplot.scatter(x_vals, y_vals, s=2, marker=".", facecolors=c_arr, edgecolors="none", alpha=0.5)

Tried with: Matplotlib 1.3.1 and Ubuntu 14.04

Markers of Matplotlib

Markers in Matplotlib
Markers in Matplotlib

Markers are used in Matplotlib plots to mark the points on a line which refers to a datapoint. All the possible marker styles in Matplotlib are listed here.

However, it is difficult to pick a marker just by looking at its character or its label. I generated the above image as a visual reference to pick markers. It was generated by using this piece of code:

Tried with: Matplotlib 1.3.0 and Ubuntu 12.04 LTS

Matplotlib: Customize number of markers shown in legend

Matplotlib is a plotting library written in Python that can be used to create all sorts of plots. If markers are being used in the plot, then two markers at the end points of a line are shown in the legend.

You may want to customize the number of markers shown in the legend. For example, I like to have one marker shown in the middle of the line segments displayed in the legend.

To customize the number of markers shown in the legend assign that value to the named argument numpoints in the call to pyplot.legend():

matplotlib.pyplot.legend( ..., numpoints=1 )

Tried with: Matplotlib 1.2, Python 3.2.3 and Windows 7 x64

Matplotlib: Set marker for line plot

Matplotlib is a plotting library written in Python that can be used to create line plots. By default, all the values are drawn with no marker symbol.

To set a marker assign it to the named argument marker in the call to pyplot.plot():

matplotlib.pyplot.plot( ..., marker="^" )

The list of all supported line styles and their usage can be found here.

Tried with: Matplotlib 1.2, Python 3.2.3 and Windows 7 x64