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.xlabel("X")
plt.ylabel("Y")
plt.title("Random plot")
plt.show()

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

random data set plotted with matplotlib in python

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

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

Leave a Reply

%d bloggers like this: