0% found this document useful (0 votes)
4 views6 pages

35 Scatterplot

The document provides an overview of creating scatter plots in Python using the Matplotlib library. It explains the syntax and parameters of the scatter() method, along with several examples demonstrating different uses of scatter plots. Additionally, it details the various options for customizing marker size, color, and style in scatter plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

35 Scatterplot

The document provides an overview of creating scatter plots in Python using the Matplotlib library. It explains the syntax and parameters of the scatter() method, along with several examples demonstrating different uses of scatter plots. Additionally, it details the various options for customizing marker size, color, and style in scatter plots.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Scatter Plot () in 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.

import matplotlib.pyplot as plt

x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]

y =[99, 86, 87, 88, 100, 86,


103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y, c ="blue")

# To show the plot


plt.show()

Example 2: Scatter plot with different shape and colour for two datasets.
import matplotlib.pyplot as plt

# dataset-1
x1 = [89, 43, 36, 36, 95, 10,
66, 34, 38, 20]

y1 = [21, 46, 3, 35, 67, 95,


53, 72, 58, 10]

# dataset2
x2 = [26, 29, 48, 64, 6, 5,
36, 66, 72, 40]

y2 = [26, 34, 90, 33, 38,


20, 56, 2, 47, 15]

plt.scatter(x1, y1, c ="pink",


linewidths = 2,
marker ="s",
edgecolor ="green",
s = 50)

plt.scatter(x2, y2, c ="yellow",


linewidths = 2,
marker ="^",
edgecolor ="red",
s = 200)

plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Example3 :

import matplotlib.pyplot as plt


girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.scatter(grades_range, girls_grades, color='r')
ax.scatter(grades_range, boys_grades, color='b')
ax.set_xlabel('Grades Range')
ax.set_ylabel('Grades Scored')
ax.set_title('scatter plot')
plt.show()
matplotlib.pyplot.scatter
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None
, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, e
dgecolors=None, plotnonfinite=False, data=None, **kwargs)[source]
A scatter plot of y vs. x with varying marker size and/or color.
Parameters
x, yfloat or array-like, shape (n, )
The data positions.
sfloat or array-like, shape (n, ), optional
The marker size in points**2. Default
is rcParams['lines.markersize'] ** 2.
carray-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', '#7f7f
7f', '#bcbd22', '#17becf'])).
markerMarkerStyle,
default: rcParams["scatter.marker"] (default: 'o')
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.
cmapstr or Colormap,
default: rcParams["image.cmap"] (default: 'viridis')
A Colormap instance or registered colormap name. cmap is
only used if c is an array of floats.
normNormalize, default: None
If c is an array of floats, norm is used to scale the color
data, c, in the range 0 to 1, in order to map into the
colormap cmap. If None, use the default colors.Normalize.
vmin, vmaxfloat, default: None
vmin and vmax are used in conjunction with the default norm
to map the color array c to the colormap cmap. If None, the
respective min and max of the color array is used. It is an
error to use vmin/vmax when norm is given.
alphafloat, default: None
The alpha blending value, between 0 (transparent) and 1
(opaque).
linewidthsfloat 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.
plotnonfinitebool, 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
dataindexable object, optional
If given, the following parameters also accept a string s,
which is interpreted as data[s] (unless this raises an
exception):
x, y, s, linewidths, edgecolors, c, facecolor, facecolors, color
**kwargsCollection properties

You might also like