Interactive Dashboard Using Plotly And Cufflinks

Data Scientist uses many libraries to do analysis with the data and extract insight from that data in order to make a decision about something like making companies competing in a new market or improving customer satisfaction or creating new products to name a few.

Some of the most famous libraries are Matplotlib, Pandas, Seaborn, they are an incredible tool in making data visualization or analysis. But the problem is that they don’t offer interactive plots and that what we will do here using plotly and cufflinks.

Installing Libraries

The first thing you have to do before starting any python code is to make sure to install the needed libraries. In our case, we need to install plotly and cufflinks either using conda or pip depending on your Python version:

Install libraries using conda:

conda install -c plotly plotly
conda install -c conda-forge cufflinks-py

Install libraries using pip:

pip install plotly
pip install cufflinks

Getting Started

Essentially, Plotly is a company that owns this library and it offers you creating a data visualization in their server like Google Colab, but also provides you an offline version of this library as a python package.

Begin our code by importing this library and other needed libraries such as numpy to create our dataset and Pandas so you can import your data as a data-frame, also cufflinks that convert plotly visualization to a JavaScript code so you can display it on the browser.

import pandas as pd
import numpy as np

After that we import some packages from the offline version of plotly and cufflinks:

import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

As we said earlier, plotly plots are interactive with the user and that’s the power of it compared to the other plotting libraries.

To make plotly able to do that we need first to connect Jupyter Notebook to JavaScript since it uses the power of JavaScript programming language in visualization to create an interactive dashboard.

To do that make sure to run this command:

init_notebook_mode(connected=True)

After we import cufflinks we need also to make it run in the offline mode:

cf.go_offline()

Create Our Dataset

Our dataset will be like a normal distribution with a random 400 points with 100 rows and 4 columns, we do that by using the np.random.rand() function in numpy library and import it to pandas as a data-fram using the function pd.DataFrame () and lastly, specify the columns name we just named it as [“A”, “B”, “C”, “D”] inside pandas DataFram function:

df = pd.DataFrame(np.random.rand(100, 4), columns=["A", "B", "C", "D"])

We can get an overview for the dataset using the df.head() function:

df.head()
df Dataset (First One)

The Line Plot

Now, after creating our dataset we plot our data as a line plot interactive dashboard using plotly. We make use of the iplot() package that we have imported earlier from plotly.offline and integrate it with pandas df variable:

df.iplot()
Line Plot

As you see above, the data-frame has been converted to an interactive dashboard and when you hover over the graph you see values for every index point and you can zoom in any particular section by selecting it. 

And if you want to hide some lines just click on one of the four lines at the right plot (The Legend) and also you can download it as png formate picture, move the graph right or left and much more.

The Scatter Plot

The iplot() essentially is the function that responsible for converting a dataset to a nice and interactive visualization and not limited just for the line plot.

To create a scatter plot using plotly the iplot() function will take some extra arguments such as:

  • kind: The type of the plot (scatter plot for example)
  • x: The data for X-axis
  • y: The data for Y-axis
  • mode: The type of points
  • size: The size of the points (self-explanatory)

Run the following command on you Notebook:

df.iplot(kind="scatter", x="A", y="B", mode="markers", size=20)
Scatter Plot

The scatter plot will plot every x point to its corresponding y point, and just like the previous graph go ahead and interact with this visualization such as zoom in or zoom out a certain section or hover over on some points and so on.

The Bar Plot

The bar plot essentially is a graph that represents a categorical column proportionally to its corresponding values. Just like what we did with scatter plot we need to change the kind argument to “bar” in order to convert it to a bar plot. 

But before doing so, let’s create another dataset that is suitable for the bar plot because this dataset will not give us a nice bar plot visualization. So we will create a dataset with two columns (category and values) and three columns (A, B, C) using the Pandas library.

df2 = pd.DataFrame({"Category":["A", "B", "C"], "Values":[30, 40, 50]})
df2.head()
df2 Dataset (Second One)

Let’s convert this data into a bar plot using this command:

df2.iplot(kind="bar", x="Category", y="Values")
Bar Plot

The Box Plot

Box Plot essentially is a representing of a numerical group of data to its quartiles. To make a box plot using plotly just passing the value in the kind argument and we will use our first data df because it has a lot of data to show:

df.iplot(kind="box")
Box Plot

The 3D Surface Plot

Since the 3D surface plot is a diagram of a three-dimensional graph we need to create a new dataset with x, y, z variables, let’s see that in the code:

df3 = pd.DataFrame({"x":[1, 2, 3, 4, 5], "y":[30, 40, 50, 40, 30], "z":[5, 4, 3, 2, 1]})
df3.head()
df3 Dataset (Third One)

Plotting 3D surface plot needs one argument which is value “surface” in the kind argument and it can take another one:

  • colorscale: The color palette of the 3D graph
df3.iplot(kind="surface", colorscale="rdylbu")
3D Surface Plot

The graph above is a 3D plot that being said you can rotate it and play around with. It worth noting that colorscal argument is optional so no need to specifying it since it has a default colorscal.

The Spread Plot

This kind of graph is used a lot in financial data and analysis or stock data, we make use of the two columns (A and B) of our first dataset df and plot them:

df[["A", "B"]].iplot(kind="spread")
Spread Plot

After running the Notebook cell, you will get two graphs a plot and a subplot, so it is like a line plot that compares them against each other and the spread plot that displays the spread against each other.

The Bubble Plot

The bubble plot is pretty similar to the previous one which was the scatter plot except it will plot the points on a big size depending on a variable you specify like the C column in our case. See the code below:

df.iplot(kind="bubble", x="A", y="B", size="C")
Bubble Plot

The code above is quite similar to the scatter plot that has been discussed earlier except in the size argument we specified a column name instead of a particular number. This kind of graph is generally applied in plots such as happiness factor and nation reports and so on.

Conclusion

In this article, we have seen that plotly is an extremely powerful tool for making nice visualization and an interactive dashboard unlike the other libraries such as matplotlib or seaborn. I suggest visiting the official plotly documentation and see the other option that is available in this tool.


Note: This is a guest post, and the opinion in this article is of the guest writer. If you have any issues with any of the articles posted at www.pythonlearning.org please contact at asif@marktechpost.com

Leave a Reply

Your email address will not be published. Required fields are marked *