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.

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!