Super Simple Python: Plot a Random Dataset

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple way to plot a random dataset in 10 lines of Python!

For a video version:

Much like the Dice Roll Simulator, Random Number Generator, High Low Guessing Game, and some other Super Simple Python projects we’ve done, plotting a random dataset will make use of the random library. We’ll also introduce a new library, matplotlib. matplotlib is a critical library for data scientists, and the default plotting library for Python.

Before we start with the program, we’ll need to use pip to install matplotlib in the terminal. We can do that with the following command:

pip install matplotlib

Generating a Random Dataset

As always, we’ll begin our program with our imports. We’ll import the random library to generate our random dataset and matplotlib.pyplot to plot it. That’s all for imports. To plot any two-dimensional dataset, we’ll need a list of x and y values. In this example, we’ll generate 100 random integers between 0 and 10 for each axis. We’ll save our x values in a list called xs and our y values in a list called ys.

import random
import matplotlib.pyplot as plt
xs = [random.randint(0, 10) for _ in range(100)]
ys = [random.randint(0, 10) for _ in range(100)]

Plotting a Random Dataset using MatPlotLib

Once we’ve generated our xs and ys all we need to do is use matplotlib to plot them. Earlier we imported matplotlib.pyplot as plt by convention. This allows us to call the module by using the name plt instead of its full name. We’ll call the scatter function to plot the xs and ys. It’s not strictly necessary to put in the xlabel, ylabel, and title, but I did because it makes the graph look nicer. Once we plot the dataset, we just have to call the show function to see it.

plt.scatter(xs, ys)
plt.title("Random plot")

When we run our program we should see something like the image below.

random data set plotted with matplotlib in python

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

2 thoughts on “Super Simple Python: Plot a Random Dataset

Leave a Reply

%d bloggers like this: