Another commonly used plot type is the simple scatter plot, a close cousin of the line plot. Instead of points being joined by line segments, here the points are represented individually with a dot, circle, or other shape. We’ll start by setting up the notebook for plotting and importing the functions we will use:
In the previous section we looked at
ax.plot to produce line plots.
It turns out that this same function can produce scatter plots as well:
The third argument in the function call is a character that represents the type of symbol used for the plotting. Just as you can specify options such as
'--' to control the line style, the marker style has its own set of short string codes. The full list of available symbols can be seen in the documentation of
plt.plot, or in Matplotlib's online documentation. Most of the possibilities are fairly intuitive, and we'll show a number of the more common ones here:
For even more possibilities, these character codes can be used together with line and color codes to plot points along with a line connecting them:
Additional keyword arguments to
plt.plot specify a wide range of properties of the lines and markers:
This type of flexibility in the
plt.plot function allows for a wide variety of possible visualization options.
For a full description of the options available, refer to the
A second, more powerful method of creating scatter plots is the
plt.scatter function, which can be used very similarly to the
The primary difference of
plt.plot is that it can be used to create scatter plots where the properties of each individual point (size, face color, edge color, etc.) can be individually controlled or mapped to data.
Let's show this by creating a random scatter plot with points of many colors and sizes.
In order to better see the overlapping results, we'll also use the
alpha keyword to adjust the transparency level:
Notice that the color argument is automatically mapped to a color scale (shown here by the
colorbar() command), and that the size argument is given in pixels.
In this way, the color and size of points can be used to convey information in the visualization, in order to visualize multidimensional data.
For example, we might use the Iris data from Scikit-Learn, where each sample is one of three types of flowers that has had the size of its petals and sepals carefully measured: