Data Visualisation: 3 Secret Tips on Python to Make Interactive Graphs and Impress Your Boss
Sajid Lhessani
Founder @ Silver Ledgers | Algorithmic Trading & Artificial Intelligence
Within with this article, we will try to enhance Python possibilities to make your graph interactive, good looking and let your boss impressed. The goal behind these tips is to give a better impression and enhance the user/client experience. It works!
As a summary, the 3 goals will be:
These three tips are coming from my experience in front of clients after working for 3 years as a data scientist and 2 years as a hedge fund manager. If you have other ideas feel free to share in the comment.
In this article, we will cover Tips #1 and #2.
Let’s get started.
0. Prerequisite
Before to start, make sure you installed a Python3 version and the following packages:
The code below will not run with Python 2
Once you have ensured that the packages above are installed, we are going to use Pandas DataReader to import market data. In this case, we are going to import data for Tesla.
If any of these packages are not already installed, you can use the pip command, as shown below.
pip install pandas-datareader
pip install DateTime
If you want to read more on algorithmic trading and how to get market data, I highly recommend this brilliant article:
If you are too lazy to read, you can find the lines of code below:
Once you have uploaded the data from Tesla, the output and the dataset which will be used is the following:
Now that the prerequisites are solved, we can start by the first tips.
Tips 1: Adding a range slider
The first tip that I am going to share with you is the range slide selector. Believe it or not, in one line of code, you can already bring substantial interactivity to your graph.
Instead of having a static graph, the user will be able to choose and zoom on a specific time period.
In the case below, it will be oriented to a financial graph, but you can use it as soon as you have time series.
For instance, it can be used for marketing to plot sales over time, or medical engineering to plot recovery evolution. I cannot cover all the different fields, but you got my point about the possibilities of applications.
Let’s type the code below:
Output:
Impressive no?
If you look at the graph above, you can found out that a range slide selector has been added to the graph. The user/client will be able to zoom in or zoom out on a specific part of the graph easily.
Another example where you can import multiple shares as well, and index these shares to the same scale (Source 1). Result below:
领英推荐
Tips 2: Add an interactive button
The second tip, I want to share with you, is the interactive filtering button.
I am sure you never heard of it.
But, when I discovered it, my life skyrocketed.
The interactive filtering, will helps the client/users to filter data instinctively or to zoom directly on a specific time or part of the graph that you want to highlight.
Again, I built it for financial data, but it can showcase any type of time series.
To build the graph above, the sol lines of code that we will need to add to our graphs are the following lines:
It looks dense, but once you type it one time, you can just have fun and copy-paste it to all your graphs.
Once we have the code block, the only variable that we can alter are the following ones:
You can even mix it with the first tip to make your audience ecstatic.
On the image below I tailored the names on the period I wanted and the final result, coupled with the range selector will look similar to that:
You can even export it as an HTML with the line of code below and send to a client or a friend.
fig.write_html("/Users/Desktop/MyGraph.html")
If you want to build it from A to Z, follow this step-by-step video, it is even more breathtaking in real-time:
If you have any questions or you want me to cover a specific point on data visualisation, feel free to publish a comment in the Q&A.
Happy Coding
Sajid Lhessani
Source:
Learn Algo Trading in One Day(Module 3):
If you want so start with programming (FREE):
Download the code: