Перейти к содержимому

Как построить scatter plot в python

  • автор:

Data Analyst’s recipe | How to create a scatter plot in Python

Nilimesh Halder, PhD

Creating a scatter plot in Python is a straightforward process that involves using a plotting library such as Matplotlib. In this recipe, we will go through the steps of creating a scatter plot in Python.

Step 1: Install Matplotlib

Matplotlib is a popular data visualization library in Python. To install it, open your command prompt or terminal and run the following command:

Step 2: Import Matplotlib and Numpy

To use Matplotlib for data visualisation, you need to import it into your Python code. Additionally, we'll also import the NumPy library to create some sample data to plot:

Step 3: Load your dataset

In this example, we will use the "Iris" dataset from the UCI Machine Learning Repository. We can load the dataset using the pandas library as follows:

Step 4: Create Scatter Plot

To create a scatter plot using Matplotlib, we need to provide x and y coordinates for each point we want to plot. In this example, we will plot the sepal length and sepal width for the first 50 rows of the dataset.

This will create a scatter plot of sepal length vs sepal width for the first 50 rows of the Iris dataset.

Simple Scatter Plots

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:

Scatter Plots with plt.plot ¶

In the previous section we looked at plt.plot / 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 plt.plot documentation.

Scatter Plots with plt.scatter ¶

A second, more powerful method of creating scatter plots is the plt.scatter function, which can be used very similarly to the plt.plot function:

The primary difference of plt.scatter from 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:

We can see that this scatter plot has given us the ability to simultaneously explore four different dimensions of the data: the (x, y) location of each point corresponds to the sepal length and width, the size of the point is related to the petal width, and the color is related to the particular species of flower. Multicolor and multifeature scatter plots like this can be useful for both exploration and presentation of data.

Как построить scatter plot в python

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It is used for plotting various plots in Python like scatter plot, bar charts, pie charts, line plots, histograms, 3-D plots and many more. We will learn about the scatter plot from the matplotlib library.
Note: For more information, refer to Python Matplotlib – An Overview

matplotlib.pyplot.scatter()

Scatter plots are used to observe relationship between variables and uses dots to represent the relationship between them. The scatter() method in the matplotlib library is used to draw a scatter plot. Scatter plots are widely used to represent relation among variables and how change in one affects the other.
Syntax
The syntax for scatter() method is given below:

matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)

The scatter() method takes in the following parameters:

  • x_axis_data- An array containing x-axis data
  • y_axis_data- An array containing y-axis data
  • s- marker size (can be scalar or array of size equal to size of x or y)
  • c- color of sequence of colors for markers
  • marker- marker style
  • cmap- cmap name
  • linewidths- width of marker border
  • edgecolor- marker border color
  • alpha- blending value, between 0 (transparent) and 1 (opaque)

Except x_axis_data and y_axis_data all other parameters are optional and their default value is None. Below are the scatter plot examples with various parameters.
Example 1: This is the most basic example of a scatter plot.

matplotlib.pyplot.scatter#

A scatter plot of y vs. x with varying marker size and/or color.

Parameters : x, y float or array-like, shape (n, )

The data positions.

s float or array-like, shape (n, ), optional

The marker size in points**2 (typographic points are 1/72 in.). Default is rcParams[‘lines.markersize’] ** 2 .

c array-like or list of colors or color, optional

The marker colors. Possible values:

A scalar or sequence of n numbers to be mapped to colors using cmap and norm.

A 2D array in which the rows are RGB or RGBA.

A sequence of colors of length n.

A single color format string.

Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. If you want to specify the same RGB or RGBA value for all points, use a 2D array with a single row. Otherwise, value-matching will have precedence in case of a size matching with x and y.

If you wish to specify a single color for all points prefer the color keyword argument.

Defaults to None . In that case the marker color is determined by the value of color, facecolor or facecolors. In case those are not specified or None , the marker color is determined by the next color of the Axes ‘ current "shape and fill" color cycle. This cycle defaults to rcParams["axes.prop_cycle"] (default: cycler(‘color’, [‘#1f77b4’, ‘#ff7f0e’, ‘#2ca02c’, ‘#d62728’, ‘#9467bd’, ‘#8c564b’, ‘#e377c2’, ‘#7f7f7f’, ‘#bcbd22’, ‘#17becf’]) ).

The marker style. marker can be either an instance of the class or the text shorthand for a particular marker. See matplotlib.markers for more information about marker styles.

cmap str or Colormap , default: rcParams["image.cmap"] (default: ‘viridis’ )

The Colormap instance or registered colormap name used to map scalar data to colors.

This parameter is ignored if c is RGB(A).

norm str or Normalize , optional

The normalization method used to scale scalar data to the [0, 1] range before mapping to colors using cmap. By default, a linear scaling is used, mapping the lowest value to 0 and the highest to 1.

If given, this can be one of the following:

An instance of Normalize or one of its subclasses (see Colormap Normalization ).

A scale name, i.e. one of "linear", "log", "symlog", "logit", etc. For a list of available scales, call matplotlib.scale.get_scale_names() . In that case, a suitable Normalize subclass is dynamically generated and instantiated.

This parameter is ignored if c is RGB(A).

vmin, vmax float, optional

When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. It is an error to use vmin/vmax when a norm instance is given (but using a str norm name together with vmin/vmax is acceptable).

This parameter is ignored if c is RGB(A).

alpha float, default: None

The alpha blending value, between 0 (transparent) and 1 (opaque).

linewidths float or array-like, default: rcParams["lines.linewidth"] (default: 1.5 )

The linewidth of the marker edges. Note: The default edgecolors is ‘face’. You may want to change this as well.

edgecolors <'face', 'none', None> or color or sequence of color, default: rcParams["scatter.edgecolors"] (default: ‘face’ )

The edge color of the marker. Possible values:

‘face’: The edge color will always be the same as the face color.

‘none’: No patch boundary will be drawn.

A color or sequence of colors.

For non-filled markers, edgecolors is ignored. Instead, the color is determined like with ‘face’, i.e. from c, colors, or facecolors.

plotnonfinite bool, default: False

Whether to plot points with nonfinite c (i.e. inf , -inf or nan ). If True the points are drawn with the bad colormap color (see Colormap.set_bad ).

Returns : PathCollection Other Parameters : data indexable object, optional

If given, the following parameters also accept a string s , which is interpreted as data[s] (unless this raises an exception):

**kwargs Collection properties

To plot scatter plots when markers are identical in size and color.

The plot function will be faster for scatterplots where markers don’t vary in size or color.

Any or all of x, y, s, and c may be masked arrays, in which case all masks will be combined and only unmasked points will be plotted.

Fundamentally, scatter works with 1D arrays; x, y, s, and c may be input as N-D arrays, but within scatter they will be flattened. The exception is c, which will be flattened only if its size matches the size of x and y.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *