LA ILAHA ILLA HU

Allah, Your Lord There Is No Deity Except Him.

# Python Data Science Matplotlib Scatter Plot the scatter() function

**How to Create Scatter Plots?**

scatter() function is used with Pyplot in order 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 1: A simple scatter plot.

code

import matplotlib.pyplot as plt

import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,94,

78,77,85,86,99])

plt.scatter(x, y)

plt.show()

the output will be

**Observations**

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

The X-axis shows how old the Bikes is.

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

Are there any relationships between the observations?

It seems that the newer the Bikes, the faster it drives, but that could be a coincidence,
given the fact is that only 13 Bikes are registered.

**Comparing 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 2: Draw two plots on the same figure

Code

import matplotlib.pyplot as plt

import numpy as np

#day 1, the age and speed of 13 Bikes:

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

plt.scatter(x, y)

#day 2, the age and speed of 15 Bikes:

x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])

y = np.array([88,87,88,111,85,103,87,

94,78,77,85,86,99])

plt.scatter(x, y)

plt.show()

the output will be

**Note: The two plots are plotted with two different colors, by default blue and orange.**

By comparing the 2 plots we can safely say that they both gives us the same conclusion.
The newer the Bike, the faster it drives.

**Setting the Colors**

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

Example 3: Set your own color of the markers.

Code

import matplotlib.pyplot as plt

import numpy as np

#day 1, the age and speed of 13 Bikes:

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

plt.scatter(x, y, color = 'hotpink')

#day 2, the age and speed of 15 Bikes:

x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])

y = np.array([88,87,88,111,85,103,87,

94,78,77,85,86,99])

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

plt.show()

the output will be

**Setting the Colors for 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 4: Set your own color of the markers.

Code

import matplotlib.pyplot as plt

import numpy as np

#day 1, the age and speed of 13 Bikes:

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

colors = np.array(["magenta","green","blue",

"yellow","pink","black","orange","purple",

"beige","brown","gray","cyan","red"])

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

#day 2, the age and speed of 15 Bikes

x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])

y = np.array([88,87,88,111,85,103,87,

94,78,77,85,86,99])

colors = np.array(["magenta","green","blue",

"yellow","pink","black","orange","purple",

"beige","brown","gray","cyan","red"])

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

plt.show()

the output will be

**ColorMap**

There are a number of options available for colormaps in Matplotlib module.

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

Example 5: Colormap

Code

Here is an example of a colormap.

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 of the point in the scatter plot.

Example 6: Create a color array, and specify a colormap in the scatter plot.

Code

import matplotlib.pyplot as plt

import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

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()

the output will be

**How to include the colormap in the drawing?**

Tthe Colormap can be includedin the drawing by including the plt.colorbar() statement.

Example 7: Include the actual colormap.

Code

import matplotlib.pyplot as plt

import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

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()

the output will be

**Available Options For ColorMaps**

You can choose any of the built-in colormaps.

Click Here to See the Options Available For ColorMaps

**How to Change the 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 8: Set your own size for the markers.

Code

import matplotlib.pyplot as plt

import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

sizes = np.array([20,50,100,200,500,1000,

60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes)

plt.show()

the output will be

**Alpha Argument**

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 9: Set your own size for the markers.

Code

import matplotlib.pyplot as plt

import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])

y = np.array([86,87,88,111,86,103,87,

94,78,77,85,86,99])

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()

the output will be

**Combine Color Size and Alpha**

You can combine a colormap with different sizes on the dots.

This is best visualized if the dots are transparent.

Example 10: Create random arrays with 200 values for x-points, y-points, colors and sizes.

Code

import matplotlib.pyplot as plt

import numpy as np

x = np.random.randint(200, size=(200))

y = np.random.randint(200, size=(200))

colors = np.random.randint(200, size=(200))

sizes = 10 * np.random.randint(200, size=(200))

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()

plt.show()

the output will be

LA ILAHA ILLA HU

Allah, Your Lord There Is No Deity Except Him.

**Current Date & Local Time Now**

**Hello Welcome , Guest Login SignUp URL Shortener Weather Get Geo City Information of Your City Lat, Long, ISO2 Country Code, Population of Most Prominent Popular Cities of the World Machine Learning Model Play Not to Play-Live Contact Us**

Bitwise Operators In Python If elif else Statements In Python Build Your Own Python Calculator The Range Function In Python while Loops In Python for Loops In Python List In Python

Break And Continue Tuples In Python Sets In Python Dictionaries In Python Functions in Python Exploratory Data Analysis - EDA Types of Data Distribution In Python Python Current Time App

# Python Tutorials On the Topics of Python Library Python Module Data Science Data Analysis Data Analytics & Machine Learning

#### Machine Learning Models Of Popular Kaggle Data Set

Asteroid Diameter Prediction Case Study 1-MACHINE LEARNING MODEL Avito Ad Demand Prediction Case Study 2 Using Neural Networks and RNN Machine Learning Model Titanic Disaster Survival Prediction Machine Learning Model- Live Abalone Age Prediction Machine Learning Model- Live AI Based Height And Weight Prediction App Live#### Matplotlib

Introduction Plotting PyPlot Markers Line Grid SubPlot Scatter Plot Bars Histograms Labels And Titles Pie Charts Matplotlib Colormap Options#### NumPy

NumPy Intro Creating Arrays Array Indexing Array Slicing Data Types Copy VS View Array Shape Array Reshape Array Iterating Array Join Array SplitArray Search Array Sort Array Filter

#### NumPy Random

Introduction Data Distribution Permutation Seaborn Normal Data Distribution Binomial Data Distribution Poisson Data Distribution Uniform Data DistributionLogistic Data Distribution Multinomial Data Distribution Exponential Data Distribution Chi Data Distribution Rayleigh Data Distribution Pareto Data Distribution Zipf Data Distribution

#### NumPy ufunc

Intro Create Your Own ufunc Simple Arithmetic Rounding Decimals Logs NumPy Summations Products Differences NumPy LCM Lowest Common MultipleNumPy GCD Greatest Common Denominator Trigonometric Functions Hyperbolic_Functions Set Operations

#### Pandas

Introduction to Pandas Pandas Series Dataframes Pandas Read CSV Pandas Read JSON Analyzing Dataframes Pandas Data Cleaning Pandas Cleaning Wrong DataPandas Cleaning Wrong Format Pandas Removing Duplicates