Photo by Luke Chesser on Unsplash

Part 1: Beginner Friendly Data Visualization using Python, Pandas and Plotly

Almas Myrzatay
5 min readSep 12, 2022

--

In this article we will explore how to visualize data using open-source, interactive library — Plotly. This charting library is dynamic and web friendly, making it an indispensable tool used for data analytics.

This article is broken into two parts. Part 1 will focus on introduction, Python environment set up and a simple example. Part 2 will have two examples focused on dynamic visualization and using real life data fetching using API.

Feel free to skip around depending on your comfort level or go straight to GitHub for copy/paste of the source code.

Table of Content

  1. Part 1: Introduction & Motivation
  2. Part 1: Set up Python environment & Python Library Installation
  3. Part 1: Simple Example - Creating Simple Visualization
  4. Part 2: Simple Example - Creating Dynamic Visualization
  5. Part 2: More Advanced Example - Fetching data via API and visualizing dynamically
  6. Part 2: Conclusion & References

1. Introduction & Motivation

Many data scientists and analysts at some point have used Matplotlib library to visualize their data. However the graphs are usually static, which means that you need to re-render in order to visualize updated results.

Plotly provides dynamic data visualization in the web browser making it an appealing choice in the world where fast-paced development coupled with web accessibility is a necessity.

Note, for this article I am assuming you are running either MacOS or Linux platform. If you are using Windows, the steps are similar, but commands are different.

2. Set up Python Environment & Plotly Installation

First, before we proceed with visualization, we need to set up Python development environment & install necessary libraries

Setting up Python environment

  1. Open your source code editor (e.g. Visual Studio Code) and terminal to create empty directory where you will host your virtual environment and source code. We will name ours as Data_Visualization
mkdir Data_Visualization

Create source directory to host all of your code

mkdir src

2. Install PIP — Package Installer for Python

Check if pip is installed by checking version

python3 -m pip --version

Skip next step if above step indicates pip is installed and up to date. Look out for terminal output message, which will prompt necessary steps if update is needed or pip is not installed.

python3 -m pip install --user --upgrade pip

3. Install python virtual environment manager if not found, or skip otherwise

python3 -m pip install --user virtualenv

Note: To learn more about motivation of why we need virtual environment manager, click here.

4. Now create your virtual environment by running this command:

python3 -m venv data-viz

Note: data-viz is the name of the virtual environment we choose for this project. You can name it whatever you want.

4. Activate virtual environment

source data-viz/bin/activate

More information regarding above steps can be found in the official Python docs here or in this article here.

At this point, you should have executed following commands and have following terminal output, which indicates your virtual environment is set up and ready to have libraries installed within a scope of this project without breaking anything globally on your computer

Sample terminal output

Installing libraries

  1. Run the following commands while in your virtual environment:
pip install pandas
pip install plotly
pip install dash
pip install requests

Note: you can also specify specific versions of these libraries if needed be. For example: pip install pandas==1.4

2. You can always verify what libraries are installed (and their versions) within your project by running following command:

pip freeze

3. I would also recommend exporting your library list into .txt file to keep track. Additionally, it is good way to have requirements.txt file within root directory of your project within git so that others know what dependencies you have. To do so, navigate to your root project directory and run:

pip freeze > requirements.txt

Now, your directory should look like this and contain following files and folders:

Folder structure of the project

Now, we are ready to create first source files and visualize data

3. Simple Example - Creating Simple Visualization

Create empty Python file and open it:

touch simple_ex.py

Insert following code inside the file (link to GitHub for copy/paste):

simple_ex.py

Let’s discuss above snippet:

Lines 1–2: import libraries and renaming them with short-hand notations using as keyword

Line 4: Pandas in the backend uses Matplotlib as default visualizer. Since Pandas 4.8, it supports Plotly as one of the default. On this line we are overriding Matplotlib by Plotly as default

Line 6: Creating dummy dataset using pandas

Lines 7–8: Creating simple plot with title and labels of the graph

Line 9: Display the graph in your default web browser (e.g. Chrome) after your run the script. It will open new tab pointed to your localhost (i.e. on your URL you will see 127.0.0.1:<port number>)

Execute following command to display the graph:

python3 simple_ex.py

The result will open in your default browser as static website. Note that even though the graph won’t be updated dynamically in this example we have shown, you can still zoom in/out, reset the size, export as png, etc… within web browser. Screenshot below shows the output:

Simple example output in web browser

In this part, we learned

  1. Set up Python environment and installing Python libraries
  2. Created simple static dashboard using Plotly. Even though, the data was static (i.e. data didn’t change as we updated source code), we still can perform various operations on the data such as zoom in/out and export it as png.

In Part 2, we will show how to display data as we update our source code, and have an example which will make an API call to fetch real data and display it.

Thanks for reading my article!

Check out my other stories and make sure to follow me for more beginner friendly tech content!

If you liked it, or have any comments/questions, let me know! Feel free to connect on social media: Instagram, LinkedIn

--

--