Python Matplotlib Tutorial

Matplotlib Scatter Plot – plt.scatter() | Python Matplotlib Tutorial

Matplotlib Scatter Plot

In the matplotlib scatter plot blog will discuss, how to draw a scatter plot using python matplotlib plt.scatter() function. The plt.scatter() function help to plot two-variable datasets in point or a user-defined format. Here, we will be plotting google play store apps scatter plot.

Import Libraries

import matplotlib.pyplot as plt # For visualization
import pandas as pd # for data cleaing & analysis

Import Dataset

# Dataset taken from kaggel.com
df_google_play_store_apps = pd.read_csv("D:\\Private\Indina AI Production\Kaggel Dataset\google-play-store-apps\googleplaystore.csv")
df_google_play_store_apps.shape # Get shape of dataset in rows and colms
Output >>>
          (10841, 13) # (rows, colomns) of data frame

The data frame contains 10841 rows and we wants only 1000 rows , so for use nrow parameter of pd.DataFrame(). You can work on hole dtaset its your choice.

df_google_play_store_apps = pd.read_csv("D:\\Private\Indina AI Production\Kaggel Dataset\google-play-store-apps\googleplaystore.csv", 
                                        nrows = 1000)
df_google_play_store_apps.shape ## Get shape of dataset in rows and colms
Output >>>
          (1000, 13) # (rows, colomns) of data frame

View Data Frame Sample 

df_google_play_store_apps

Output >>>

Google Play Store Apps Data Frame

The data frame contain numeric and string typle value and for scatter plot need numeric value. So, we are ploting scatter plot of Google Play Store Apps Data Frame 1as x and Reviews as y coloms data.

x = df_google_play_store_apps["Rating"]
y = df_google_play_store_apps["Reviews"]

Plot Scatter plot using matplotlib

Syntax : plt.scatter(

                                      x,
                                      y,
                                      s=None,
                                      c=None,
                                      marker=None, 
                                      cmap=None,
                                      norm=None,
                                      vmin=None,
                                      vmax=None,
                                      alpha=None,
                                      linewidths=None,
                                      verts=None, ### same work like – marker
                                      edgecolors=None,
                                      *,
                                      data=None,
                                      **kwargs,
                                      )

plt.scatter(x,y) # Draw scatter plot with parameter x and y
plt.show()

Output >>>

Matplotlib Scatter Plot
Fig 1.1 – Matplotlib Scatter Plot

Show scatter plot with title, xlabel and ylabel.

plt.scatter(x,y)
plt.title("Google Play Store Apps Scatter plot")
plt.xlabel("Rating")
plt.ylabel("Reviews")
plt.show()

Output >>>

Matplotlib Scatter Plot
Fig 1.1 – Matplotlib Scatter Plot with functions

Ploting Scatter plot with parameters 

plt.figure(figsize = (16,9)) # figure size ratio 16:9
plt.scatter(x,y, c = "r", marker = "*", s = 100, alpha=0.5, linewidths=10,
           edgecolors="g" )#verts="<"
plt.title("Google Play Store Apps Scatter plot")
plt.xlabel("Rating")
plt.ylabel("Reviews")
plt.show()

Output >>>

Matplotlib Scatter Plot with parameters
Fig 1.3 – Matplotlib Scatter Plot

Plot Two Scatter Plot in one plot

plt.figure(figsize = (16,9))
plt.scatter(x,y, c = "r", marker = "*", s = 100, alpha=0.5, linewidths=10,
           edgecolors="g" )#verts="<"

plt.scatter(x,df_google_play_store_apps["Installs"], c = "y", marker = "o", s = 100, alpha=0.5, linewidths=10,
           edgecolors="c" )
plt.title("Google Play Store Apps Scatter plot")
plt.xlabel("Rating")
plt.ylabel("Reviews & Installs")
plt.show()

Output >>>

Matplotlib two scatter plot
Fig 1.4 – Matplotlib two scatter plot

Conclusion

In the matplotlib plt.scatter() plot blog, we learn how to plot one and multiple scatter plot with a real-time example using the plt.scatter() method. Along with that used different method and different parameter. We suggest you make your hand dirty with each and every parameter of the above methods. This is the best coding practice. After completion of the matplotlib tutorial jump on Seaborn.

Download Jupyter file of matplotlib scatter plot source code

Visit the official site of matplotlib.org

1 thought on “Matplotlib Scatter Plot – plt.scatter() | Python Matplotlib Tutorial”

  1. Nand Prakash Gautam

    I would like to appreciate your explanation. I Have question regarding when i am try to execute following code “plt.figure(figsize = (16,9))
    plt.scatter(x,y, c = “r”, marker = “*”, s = 100, alpha=0.5, linewidths=10,
    edgecolors=”g” )#verts=”<"

    plt.scatter(x,df_google_play_store_apps["Installs"], c = "y", marker = "o", s = 100, alpha=0.5, linewidths=10,
    edgecolors="c" )
    plt.title("Google",fontsize=18)
    plt.xlabel("Rating",fontsize=13)
    plt.ylabel("Reviews",fontsize=13)
    plt.show()"
    Not showing proper graph. Looking this type "https://photos.google.com/photo/AF1QipNd9YDNmu7fd01DXtd27e7Ff5Xu_juMf6eJRbJV&quot; I did analyses but unable to under stand why this showing this type.
    Thanks & Regards
    Nand Prakash Goutam

Leave a Reply