Basic Matplotlib Scatterplot

logo of a chart:ScatterPlot

This post describes how to draw a basic scatterplot using matplotlib library. Once you understood the function, you can see the next post to customise your scatterplot chart.

Libraries & Dataset

Let's start by loading the necessary libraries and create a dataset

  • matplotlib: for displaying the plot
  • pandas: for data manipulation
  • numpy: for data generation
# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Create a dataset:
df = pd.DataFrame({
    'x_values': range(1, 101),
    'y_values': np.random.randn(100)*15+range(1, 101)
})
df.head()
x_values y_values
0 1 16.036968
1 2 -11.666240
2 3 8.503309
3 4 7.816552
4 5 -0.893881

Most simple scatter plot

This is a basic scatterplot example made with the scatter() function of Matplotlib. These arguments are passed to the function:

  • x : column name to be used for the x axis
  • y : column name to be used for the y axis
  • data : the dataset to be used
  • linestyle : style of the lines between each point
  • marker : marker style of the points
fig, ax = plt.subplots()
ax.scatter(
    'x_values', 'y_values',
    data=df,
    marker='o'
)
plt.show()

Going further

This post explains how to create a scatter plot with Matplotlib.

You might be interested in how to custom markers in scatter plots and how to link title and markers in scatter plots.

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    dataviz decision tree poster