GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Basic Scatterplot with Seaborn

Basic Scatterplot with Seaborn

logo of a chart:ScatterPlot

A scatterplot is a type of chart that shows the relationship between two numerical variables. Each member of the dataset gets plotted as a point whose x-y coordinates relates to its values for the two variables.

This example shows how to create a scatterplot with seaborn and how to add a regression line.

Scatterplot sectionAbout this chart

Libraries

First, we need to load a few libraries:

  • seaborn: for creating the scatterplot
  • matplotlib: for displaying the plot
  • pandas: for data manipulation
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

Dataset

Since scatter plot are made for visualizing relationships between two numerical variables, we need a dataset that contains at least two numerical columns.

Here, we will use the iris dataset that we load directly from the gallery:

path = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/iris.csv'
df = pd.read_csv(path)
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
Dataviz caveat card: Beware of overplotting

⚠️ Avoid the 61 most common dataviz mistakes

The dataviz caveats I see the most, as a deck of 61 cards: a chart that is not ideal, and a better alternative. Free.

I respect your privacy. Unsubscribe anytime.

Scatter plot

A scatterplot can be made using scatterplot() function of seaborn library. An example dataset from seaborn repository, iris dataset, is used in the example. The plot shows the relationship between sepal lenght and width of plants. In order to show the most basic utilization of this function, the following parameters should be provided:

  • x : positions of points on the X axis
  • y : positions of points on the Y axis

Basic Scatterplot with Seaborn: Scatter plot

sns.scatterplot(x=df["sepal_length"], y=df["sepal_width"])
plt.show()

Alternatively: you can use the following syntax:

Basic Scatterplot with Seaborn: Scatter plot (2)

sns.scatterplot(data=df, x="sepal_length", y="sepal_width")
plt.show()

Customize the scatter plot

The scatterplot() function accepts the following arguments (among others):

  • color: of the points
  • edgecolor : edge colour of the points
  • alpha : transparency of points (between 0 and 1)
  • s: dot size

You can find all the available colors here.

Basic Scatterplot with Seaborn: Customize the scatter plot

sns.scatterplot(
    x=df["sepal_length"],
    y=df["sepal_width"],
    color="red",
    edgecolor="darkred",
    alpha=0.6,
    s=200
)
plt.show()

Going further

This post explains how to create a scatterplot with seaborn.

You might be interested in more advanced examples on

  • how to visualize linear regression
  • how to color points according to a third variable.
  • discover more marker customization.

Related posts

Selecting Color Palettes with Seaborn

Selecting Color Palettes with Seaborn

Custom a Matplotlib Scatterplot

Custom a Matplotlib Scatterplot

Use categorical variable to color scatterplot in seaborn

Use categorical variable to color scatterplot in seaborn

Control Axis Limits of Plot

Control Axis Limits of Plot

Control Color of Each Marker

Control Color of Each Marker

Add text annotation on scatterplot

Add text annotation on scatterplot

Related chart types

logo of a chart:ScatterPlot

Scatterplot

logo of a chart:Heatmap

Heatmap

logo of a chart:Correlogram

Correlogram

logo of a chart:BubblePlot

Bubble

logo of a chart:ScatterConnected

Connected Scatter

logo of a chart:2dDensity

2D Density

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.