• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Python Tips / How To Make Scatter Plot in Python with Seaborn?

How To Make Scatter Plot in Python with Seaborn?

April 8, 2019 by cmdlinetips

Scatter plots are a useful visualization when you have two quantitative variables and want to understand the relationship between them.

In this post we will see examples of making scatter plots using Seaborn in Python. We will first make a simple scatter plot and improve it iteratively.

Let us first load the packages we need to make scatter plots in Python.

# import pandas
import pandas as pd
# import matplotlib
import matplotlib.pyplot as plt
# import seaborn
import seaborn as sns
%matplotlib inline

We will use the gapminder data to make scatter plots. Let us load the gapminder data from Software Carpentry github page.

data_url = 'http://bit.ly/2cLzoxH'
# read data from url as pandas dataframe
gapminder = pd.read_csv(data_url)
print(gapminder.head(3))

We can make scatter plots using Seaborn in multiple ways. Let us use Seaborn’s regplot to make a simple scatter plot using gapminder data frame.

We will be using gdpPercap on x-axis and lifeExp on y-axis. Seaborn’s regplot takes x and y variable and we also feed the data frame as “data” variable. We also specify “fit_reg= False” to disable fitting linear model and plotting a line.

sns.regplot(x="gdpPercap", y="lifeExp",
            data=gapminder,fit_reg=False)
Scatter Plot with Seaborn Python
Scatter Plot with Seaborn Python

We can also get the same scatter plot as above, by directly feeding the x and y variables from the gapminder dataframe as shown below.

sns.regplot(x=gapminder["gdpPercap"], y=gapminder["lifeExp"],
            fit_reg=False)

How to Add Log Scale to Scatter Plot in Python?

Out first attempt at making a scatterplot using Seaborn in Python was successful. However, if you look at the scatter plot most of the points are clumped in a small region of x-axis and the pattern we see is dominated by the outliers.

A better way to make the scatter plot is to change the scale of the x-axis to log scale. To make the x-axis to log scale, we first the make the scatter plot with Seaborn and save it to a variable and then use set function to specify ‘xscale=log’.

splot = sns.regplot(x="gdpPercap", y="lifeExp", 
                    data=gapminder, fit_reg=False)
splot.set(xscale="log")
Scatter Plot With Log Scale Seaborn Python
Scatter Plot With Log Scale Seaborn Python

We see a linear pattern between lifeExp and gdpPercap. Now, the scatter plot makes more sense. However, a lot of data points overlap on each other. It will be nice to add a bit transparency to the scatter plot.

We can use scatter_kws to adjust the transparency level using a dictionary with key “alpha”.

splot = sns.regplot(x="gdpPercap", y="lifeExp", 
                    data=gapminder,
                    scatter_kws={'alpha':0.15},
                    fit_reg=False)
splot.set(xscale="log")

Adjusting Transparency in Scatter Plot
Scatter Plot with Transparency

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

How To Specify Colors to Scatter Plots in Python Default ThumbnailHow To Make Histogram in Python with Pandas and Seaborn? Plot with two different y-axis with twinx in PythonHow to Make a Plot with Two Different Y-axis in Python with Matplotlib ? Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R

Filed Under: Python Tips, Scatter Plot with Python, Scatter Plot with Seaborn Tagged With: log scale Plot Python, Scatter Plot with Python, Scatter Plot with Seaborn

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version