Plotly: Introduction and Installation

Faran Mohammad
5 min readJul 3, 2023

The plotly Python library is an interactive, open-source plotting library that supports over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases.

Installing Plotly and running it:

  1. Install Python and Anaconda:
sudo pacman -S python
yay -S anaconda

I use Manjaro Linux, but the installation will be the same on all platforms.

2. Install the libraries:

i. Pandas

pip install pandas

pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.

ii. Numpy

pip install numpy

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

iii. Matplotlib

pip install matplotlib

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

iv. Cufflinks

conda install -c conda-forge cufflinks-py

Cufflinks is a library that binds the power of plotly with the flexibility of pandas for easy plotting.

3. After the installation is complete, launch Anaconda and launch Jupyter Notebook.

Anaconda Navigator

4. Click on the New button and select Python3 under Notebooks.

This is how we run Plotly on Jupyter Notebook.

Getting into it

For data visualization in Plotly, we need to import the libraries that we had previously installed and do some configuration:

import pandas as pd
import numpy as np
import chart_studio.plotly as py
import cufflinks as cf
import seaborn as sns
import plotly.express as ps
%matplotlib inline

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()

This code snippet is used to set up the environment for data visualization using various libraries such as Pandas, NumPy, Chart Studio Plotly, Cufflinks, Seaborn, and Plotly Express.

Let’s go through each line of the code to understand its purpose:

  1. import pandas as pd: Imports the Pandas library and assigns it the alias ‘pd’. Pandas is a powerful library for data manipulation and analysis.
  2. import numpy as np: Imports the NumPy library and assigns it the alias ‘np’. NumPy provides support for mathematical and logical operations on large, multi-dimensional arrays and matrices.
  3. import chart_studio.plotly as py: Imports the Plotly library, specifically the module chart_studio.plotly, and assigns it the alias ‘py’. Plotly is a library for creating interactive visualizations and offers an online platform for sharing and collaborating on plots.
  4. import cufflinks as cf: Imports the Cufflinks library and assigns it the alias ‘cf’. Cufflinks is a library that integrates Plotly with Pandas for easy and intuitive data visualization.
  5. import seaborn as sns: Imports the Seaborn library and assigns it the alias ‘sns’. Seaborn is a statistical data visualization library that is built on top of Matplotlib.
  6. import plotly.express as ps: Imports the Plotly Express library and assigns it the alias ‘ps’. Plotly Express is a high-level interface for creating a wide range of interactive plots.
  7. %matplotlib inline: This is a magic command specific to Jupyter notebooks, which allows the plots to be displayed directly in the notebook.
  8. from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot: Imports specific functions from the Plotly Offline module. These functions are used for configuring Plotly to work in offline mode within Jupyter notebooks.
  9. init_notebook_mode(connected=True): Initializes Plotly in the notebook and connects it to the notebook interface.
  10. cf.go_offline(): Configures Cufflinks to work offline and use the Plotly library for visualization.

“Hello, World!” in Plotly:

arr_1 = np.random.randn(50,4)
df_1 = pd.DataFrame(arr_1, columns=['A','B','C','D'])
df_1.head()
df_1.plot()
df_1.iplot()

This code snippet generates a random array using NumPy, creates a Pandas DataFrame from that array, and then displays the first few rows of the DataFrame and plots the data.

Let’s break it down step by step:

  1. arr_1 = np.random.randn(50,4): This line generates a random array with a shape of (50, 4) using the np.random.randn function from NumPy. The randn function generates random numbers from a standard normal distribution (mean = 0, standard deviation = 1). So, arr_1 is a 2-dimensional array with 50 rows and 4 columns, filled with random values.
  2. df_1 = pd.DataFrame(arr_1, columns=[‘A’,’B’,’C’,’D’]): This line creates a Pandas DataFrame named df_1 from the arr_1 array. The DataFrame constructor pd.DataFrame takes the array as the first argument and the columns parameter is used to assign column names to the DataFrame. In this case, the column names are specified as [‘A’, ‘B’, ‘C’, ‘D’].
  3. df_1.head(): This line displays the first few rows of the DataFrame df_1. The head() method is used to retrieve the top rows of the DataFrame, by default the first five rows, providing a glimpse of the data.
  4. df_1.plot(): This line generates a basic line plot of the data in the DataFrame df_1. The plot() method is a convenient way to create plots directly from a DataFrame. By default, it creates a line plot with the DataFrame’s index on the x-axis and each column as a separate line on the y-axis.
  5. df_1.iplot(): This line is for Plotly. It uses the Cufflinks wrapper over plotly that runs Matplotlib under the hood. It seems to be the easiest way to get interactive plots with simple one line code.
Output of df_1.head()
Output of df_1.plot()
Output of df_1.iplot()

Continued here.

--

--

Faran Mohammad

I am a Software Development Engineer. I like reading and writing about tech. Being a geek, I like to experiment with various technologies and stacks.