Data Visualization with Plotly

Data Visualisation plays a major role to study and analyse data to extract useful insights that can add value to our end task. Python being a universal language provides a lot of libraries for almost every task you have ever heard of. The most common libraries for data visualization in Python are Matplotlib and Plotly. Matplotlib provides a simple user experience for data visualization. But Plotly provides an interactive data visualization in Python. In this article, I will take you through an interactive data visualization with Plotly.

Plotly can be easily installed like all other packages in Python, using a very simple command in your terminal- pip install plotly. Plotly is a very powerful tool for visualization. Let’s start with it by importing the necessary libraries and scraping the dataset. If you find any problem in scraping the dataset from the code below, you can download the dataset from here.

Also, Read: Trading Strategy with Machine Learning.

import pandas as pd
df = pd.read_csv( "https://raw.githubusercontent.com/amankharwal/Website-data/master/volcano_db.csv", encoding="iso-8859-1")
df.head()Code language: Python (python)
image for post

As you can see the dataset is based on the information of volcanoes. The first insights we might be interested in to look should be the number of volcanoes we have in each country. Let’s plot a histogram, which will show us the number of volcanoes in each country:

import numpy as np
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/amankharwal/Website-data/master/volcano_db.csv", encoding="iso-8859-1")
import plotly.express as px
fig = px.histogram(df, x="Country")
fig.show()Code language: Python (python)
data visualization

This looks fine, but to have a better look let’s visualize the data in descending order:

fig = px.histogram(df, x="Country").update_xaxes(categoryorder="total descending")
fig.show()Code language: Python (python)
data visualization

If you have run the above code, you must have seen an interactive output where you can manipulate the chart. Now let’s visualize an interactive pie chart to see the importance of each volcano:

import plotly.graph_objects as go
labels=df.Status.unique()
values=df['Status'].value_counts()
fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
fig.show()Code language: Python (python)
pie chart

Interactive Geographical Data Visualisation with Plotly

Now let’s move towards the advanced level of data visualization in Python. Here I will plot the geographical location of our volcanoes. If you will have a detailed look at the dataset, you will find there are two features, longitude and latitude, which will allow you to visualize the geographical location of volcanoes:

import plotly.graph_objects as go
fig = go.Figure(data=go.Scattergeo(
        lon = df['Longitude'],
        lat = df['Latitude'],
        mode = 'markers'
        ))
fig.update_layout(
        title = 'Volcanos',
        geo_scope='world',
    )
fig.show()Code language: Python (python)
data visualization

The above visualization shows the geographical location of volcanoes on a map. Let’s plot the same features on an interactive globe:

fig = go.Figure(data=go.Scattergeo(
        lon = df['Longitude'],
        lat = df['Latitude'],
        mode = 'markers',
        showlegend=False,
        marker=dict(color="crimson", size=4, opacity=0.8))
        )
fig.update_geos(
    projection_type="orthographic",
    landcolor="white",
    oceancolor="MidnightBlue",
    showocean=True,
    lakecolor="LightBlue"
)
fig.show()Code language: Python (python)
plotly
You can even rotate this globe using your cursor

Advance Level Data Visualization using Plotly

Now, let’s do something more advanced, by creating an interactive volcano using data visualization techniques. For this I will scrap different dataset which I can use to create an interactive visualization of a volcano:

df_v = pd.read_csv("https://raw.githubusercontent.com/amankharwal/Website-data/master/volcano.csv")
fig = go.Figure(data=[go.Surface(z=df_v.values)])
fig.update_layout(title='Volcano', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))
fig.show()Code language: Python (python)
You can rotate it at 360 degrees using your cursor

As you can see, by making visualization more interactive it helps in getting more ideas as you can manipulate on the plot using a cursor. It helps in getting more insights by writing less code. I hope you liked this article on Data Visualisation with Plotly. Feel free to ask your valuable questions in the comments section below.

Follow Us:

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1537

2 Comments

  1. Hi Aman,

    Really i was surprized by this article. its completly brand new for me. You are deserved for this.Thanks alot Ji

Leave a Reply