Plot beautiful and interactive graphs using Plotly

Take your data visualisation skills to the next level using Plotly

Sanket Chavan
Analytics Vidhya
7 min readSep 6, 2021

--

Introduction

Hello guys, in this blog, we will be learning the uses of plots and how to plot them using a very good tool called Plotly. We will learn how this tool makes it very easy and fun to plot graphs. We will be writing codes for line charts, bar graphs, histograms, and many other types of graphs. The blog contains a lot of code snippets. Therefore, to get the most out of this blog, I strongly suggest that we open our Jupyter notebooks, and follow along as we start plotting the graphs.

Another option to follow along is to check out the Jupyter notebook linked here. If you click on this link, you will be redirected to the Jovian website. Jovian is a great platform for learning data science and collaboration. On the top right corner, you will see a run button. Click on the run button and choose whichever option you see fit. My personal favourite is Colab. After clicking the option, the notebook will open in the respective platform. After that, you can run the notebook yourself as you read this blog.

Why do we need graphs?

Graphs are visual representations of data. Just by looking at the graphs, we can understand the patterns in the data, which are very difficult to understand with tabular or textual representation. Graphs display information in a manner that is very easy for most people to understand. In data analytics, graphs are used to understand the relationship between variables/columns to gain insights into the data. Graphs come in very different forms with equally different uses. The type of graphs is dependent on the type of data that is being conveyed.

Plotly is a free open-source library for plotting graphs. It is used to create a data visualization that can be displayed in Jupyter notebooks. It provides a variety of graphing options, right from line charts to 3-D charts. It also works offline.

To install plotly, run the following command in your terminal or inside the code cell of your jupyter notebook

pip install plotly

We will also need another Python library, pandas, to read the example datasets into the jupyter notebooks. To install pandas, run the following command

pip install pandas

Check out the Jupyter notebook used here

Enough theory, let’s jump into coding!!

Imports

Line Graphs

Line graphs are used to plot the data which changes with time. If you want to plot your expenses on a weekly, monthly or yearly basis, the line graph is your best option. It is a basic type of chart common in many fields.

We will import the stocks , a dataset that comes with the plotly to demonstrate the line graphs. Let’s import that dataset.

The methods to plot all the graphs in plotly are very similar to each other. We will first create a simple plot and then beautify it. Here is how it is done.

Now the graph might look simple in this image, but if you execute the code cell in your jupyter notebook, the output graph will be very interactive. When you hover over the lines, you will be able to see information like dates and prices.

Bar Chart

Well, we all know what a bar graph is. Still, let’s have a look at the basic definition.

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column chart.

A bar graph shows comparisons among discrete categories. One axis of the chart shows the specific categories being compared, and the other axis represents a measured value. Some bar graphs present bars clustered in groups of more than one, showing the values of more than one measured variable.

To plot the bar graph, we will be using the gapminder dataset. In this dataset, each row represents a country in a given year. We can import that dataset in the same as we imported stocks .

Let’s plot the graph now.

A stacked bar graph (or stacked bar chart) is a chart that uses bars to show comparisons between categories of data, but with the ability to break down and compare parts of a whole. Each bar in the chart represents a whole, and segments in the bar represent different parts or categories of that whole. The example of a stacked bar chart is given in the Jupyter notebook linked above in the introduction.

Scatter Plot

A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. One additional variable can be displayed if the points are coded (colour/shape/sized. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

We will use the famous iris dataset for the scatter plots. Here is how we plot it.

Pie Chart

A pie chart (or a circle chart) is a circular statistical graphic divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents

Pie charts are very useful. They are visually simpler than other types of graphs. However, the number of slices we can display is limited.

To plot the pie chart, we will use the same gapminder the dataset we used earlier.

After plotting this graph, if we click on the labels in the legend, parts of the pie chart corresponding to that label will be omitted. This way, we can control which class data to see.

Histogram

A histogram is an approximate representation of the distribution of numerical data. Similar in appearance to a bar graph, the histogram condenses a data series into an easily interpreted visual by taking many data points and grouping them into logical ranges or bins.

To plot the histogram, we will create a custom range of values and plot them.

We can beautify the histogram using update_layout() function. Click here to see how that is done.

Box Plot

A box and whisker plot — also called a box plot — displays the five-number summary of a set of data. The five-number summary is the minimum, first quartile, median, third quartile, and maximum. Box plots can be drawn either horizontally or vertically. Box plots received their name from the box in the middle and their plot.

A box and whisker plot summarises a set of data measured on an interval scale. It is often used in explanatory data analysis. This type of graph is used to show the shape of the distribution, its central value, and its variability. Also, they can show the outliers present in the data.

We will be using the tips dataset to plot the Box plot

Violin Plot

A violin plot plays a similar role as a box and whisker plot. It shows the distribution of quantitative data across several levels of one (or more) categorical variables such that those distributions can be compared. Unlike a box plot, in which all of the plot components correspond to actual data points, the violin plot features a kernel density estimation of the underlying distribution.

We will be again using the tips dataset for the violin plot.

We can find more complex examples of a violin plot here.

Density Heatmaps

A heatmap (aka heat map) depicts values for the main variable of interest across two axis variables as a grid of coloured squares. The axis variables are divided into ranges like a bar chart or histogram, and each cell’s colour indicates the value of the main variable in the corresponding cell range

Dataset used: flights from seaborn library

Here is how to plot it.

Conclusion

That brings us to the end of this blog. We have learned a lot here.

  1. What are graphs and why use them
  2. Types of graphs and their uses.
  3. How to plot them using Plotly.

But still, there are many things to learn and explore.

We can use the graphs above in our EDA projects, ML projects or simply to gain insights into how we are performing in terms of our budgets, fitness etc.

References

The following references were used in making this blog. I highly recommend you check them out.

You can connect with me on LinkedIn using the following link:

Thank you so much!!!

--

--