Python | Scatter Plot

Python | Scatter Plot: In this tutorial, we are going to learn about the scatter plot and its implementation with examples.
Submitted by Anuj Singh, on July 07, 2020

Inherited from the Dot Plots, Scatter plots are of very similar types. It provides a power of different features for every individual point. We are allowed to vary the size, color, and other properties of each data point and which makes data more friendly to visualize.

1) Normal Scatter Plot

Syntax:

plt.scatter(x, y, alpha=0.8)

Parameter(s):

  • x, y - data points in an array
  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)
scatter plot (1)

2) Scatter Plot with variable size

Syntax:

plt.scatter(x, y, s=area, alpha=0.8)

Parameter(s):

  • x, y - data points in an array
  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)
  • s = area - the size of each point in terms of area
scatter plot (2)

3) Dash Point Plots with color code Green, Red, Blue Yellow

Syntax:

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

Parameter(s):

  • x, y - data points in an array
  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)
  • c = colors - colors of each point
scatter plot (3)


4) Star Point Plot with Color Code Blue, Green, Red, Yellow

Syntax:

plt.scatter(x, y, c=colors, s=area, alpha=0.8)

Parameter(s):

  • x, y - data points in an array
  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)
  • c = colors - colors of each point
  • s = area - size of each point in terms of area
scatter plot (4)

Applications:

  1. Machine Learning
  2. Monte Carlo Simulation
  3. Case Studies
  4. Probabilistic Models

Python code to demonstrate example of scatter plot

import numpy as np
import matplotlib.pyplot as plt

N = 40
x = np.random.rand(N)
y = np.random.rand(N)*10

# random colour for points, vector of length N
colors = np.random.rand(N)

# area of the circle, vectoe of length N
area = (30 * np.random.rand(N))**2  
# 0 to 15 point radii

# a normal scatter plot with default features
plt.scatter(x, y, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Normal Scatter Plot')
plt.show()

# a scater plot with different size
plt.figure()
plt.scatter(x, y, s=area, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Different Size')
plt.show()

# a scatter plot with different collour
plt.figure()
plt.scatter(x, y, c=colors, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Different Colour')
plt.show()

# A combined Scatter Plot
plt.figure()
plt.scatter(x, y, s=area, c=colors, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Combined')
plt.show()

Output:

Output is as figure





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.