Plotly Express - How to create different graphs for data visualisation
Creating graphs using Plotly Express

Plotly Express - How to create different graphs for data visualisation

By Sumukh | CryptoBomb | 18 Feb 2021


What is Plotly Express ?

Plotly Express is a new high level python visualization library. It has functions that can create entire graphs at once.

High quality graphs can be created with just a single line of code.

With these graphs we can just analyze the crypto market, it also enables us to compare different cryptocurrencies.

Setting up Google Colab

We can use Google Colab for practicing and creating the graphs, Google colab includes notebook where you can work and execute the codes and your work automatically gets saved.

Plotly will be already pre-installed in Google Colab.

Here, we need to go to Google Colab website and create a new notebook, give the name of your choice.

70c617bb8c60a13d5e6b308dbbec8169c385aa4f1a696e1c3087332bff151eb5.png

Here is the link to use Google Colab : https://colab.research.google.com/

After Creating a new notebook the window will look something like this,

4053f5a8a951ffb8eb2716a7e971868eae906cb302bb2e98d538d76ed20a6225.png

This is where we will write and execute the codes.

Since this graph is coded with python codes, I will explain each line of code used in this.

  • Before beginning we need to change the runtime to GPU for faster performance
  • For this, click on runtime option at the top, Runtime->Change Runtime type, then select GPU from the drop down menu, and then save.
  • After saving click on connect at the top.

08450b33238369de8b0e63bb8f1053c304d4650f38d481361493a4549d3910ca.png

 

Creating graphs 

 

Importing the neccessary libraries
  • Firstly, we need to import all the libraries. Basically libraries are files containing different codes which we need to use somewhere in our program, so to use functions from the libraries we need to import them.
  • First we will import panda, panda's are fast powerful data analysis and manipulation package we will use this to read our data and manipulate it.

->Here we are importing panda as pd, pd will be used as a nickname for panda.

  • import panda as pd 

->We will use this function to create beautiful table.

  • import plotly.offline as py

->We will use this to create candle stick charts.

  • import plotly.graph_objs as go

->We will use this to create different graphs like line graphs, box plot, violin plot, area graph and pie chart.

  • import plotly.express as px

After above codes just click on the play button on the left side of the box, which will execute and check for any errors in your code and also import all the functions, so every time you use this you need to click on play button for these in-order to import these files.

45635325c9e13c7993c2e5bdb7957dc1979b57b9ed3bef22b0913a0c799d0644.png

Initializing Plotly

Initializing ploty in this is the main step, without this your graphs or charts will not be displayed.

First click on +Code option near +Text to create another section of Code, This will create different sections for different codes, makes you easy to understand, you can also give a text to each section of code by Clicking on +Text.

We will use this section to initialize.

->First import plotly as pio.

  • import plotly.io as pio

->Use the pio to initialize the plotly in Colab.

  • pio.renderers.default='colab'

Now, run this.

13ea746335f5f6e843c543f3acdb59505bac2ad4e964245b89be6435352ad8ea.png

Importing Datasets  : Top Coins

We need to import some datasets to make our graphs, datasets are historical data or the data which is needed to create the graphs.

You can use your own dataset. However, I will add the link to download the datasets which we will be using here.

Link 1 Top Coins : http://upload-4ever.com/d/HyJl

Top coins contain 5 year historical data of 12 top crypto currencies.

  • from google.colab import files
  • files.upload()

->This is to import and upload the files after running this we will get an option to upload our files, we then have to just upload the file.

  • First upload the Top Coin csv file, I have mentioned the download link above, By uploading the Top coins we can analyze and compare different coins at once and also create graphs.
  • After Choosing the file, the file will get uploaded, to choose the file again we simply have to run the code again.

645cecc88b68d3040f317c7a9d3b9c0c823577c6ff8963793e3ace467af0d30d.png

We can view our imported dataset,

  • df= pd.read_csv("top_coins.csv")

->This will read our file "top_coins.csv" and store the values in df, we know that pd here is the nickname for panda which we have mentioned while importing panda, it is used here for reading the table. 

  • Now to view our read table we just need to type and run df
  • df

6c9e3a44709771baaa8f89428766ad6d056898c0e07b673e7ca43e8ffb47dc35.png

  • Here we can see we have 8 columns, date, open price, high price, low price and close price, volume and name of the currency.
  • Volume column tells us about daily transaction in terms of USD.
  • For the price section in the graph we will use close price because its the most accurate price among open high and low.
  • we can see that we have total 35737 rows.

 

  • We can also group by column name,
  • df.groupby(["Currency"]).count()

->This groups the table in terms of Currencies and counts the total number of rows present in it.

34e537789415161340dcc975a0c47d9dd91a8756c22d7256199321d60e7fddd3.png

  • As we can see we have grouped the table according to currency, here Bitcoin has 3652 records and Cardano has only 1080 because bitcoin is more than 5 years old.

 

Violin Plot, Box Plots and Pie Chart
1. Violin Plot :
  • Violin Plots provides us with statistical details like median, max, min.
  • Let's compare and visualize all the currencies with each other in terms of its volume of transactions using Violin plot.
  • We already have imported plotly express as px above so lets use it,
  • px.violin(df, x="Currency", y="Volume")

->The Above code is all that is required to create a violin chart px.violin is to create the chart, df is the data we need to use, x is the x-axis of our chart i.e. Currency, y is the y axis i.e. Volume.

8bc27f6b4085d2b53ec1b409926e6cd51441ea37af869f0c0217b040eefc0907.png

  • Here, we have a beautiful violin graph, we can see that bitcoin and tether dominate in terms of volume, and tether has the higher transaction volume.
  • When you hover on any crypto currencies in the graph you can see their statistical value.
2. Pie chart :
  • We can analyze the market share of cryptocurrencies in terms of its volume using pie chart.
  • px.pie(df, values="Volume", names="Currency")

->The Above code is used to create Pie chart, px.pie is to create the chart, df is the data we need to use, we are comparing the currencies in terms of Volume so values will contain Volume, and we are analyzing each cryptocurrency, so the names will be the currency column which contains the names of all the currency.

6c50ecee325d8d6d1a4723a1f712ecae7d807ad082cf40d22d1cdf16cb9ad305.png

  • We have a beautiful pie chart here, we can see tether marked in blue has highest volume at almost 36% followed by Bitcoin and Ethereum
  • When we hover over them we can see the name of the currency and its volume.
3.Box Plot :
  • For this, let us compare currencies in terms of its price, we will use box plot to visualize the price.
  • This also provides us with statistical data like median etc.
  • We will be using close price for the price.
  • px.box(df, x="Currency", y="Close")

->The above code gives us the Box plot. Here, px.box is to create a box plot, horizontal x axis will be the Currency and Vertical y axis will be the price i.e. Close.

2e33f92a83f6dbcb17850587e28136e43674673144ab63329e44291e336a191e.png

  • We can see the above Box Plot is created and the Bitcoin is clearly dominating in terms of price, it has the highest price.
  • When we hover over them we can see all the statistical data.
  • To make other smaller coins visible we can use log_y=true in our code i.e., 
  • px.box(df, x="Currency", y="Close", log_y="True")

e7c9f040d4e38f29d96dfa1919ee61a4a88efe88febea3226e2a537e66c39b46.png

  • Now, we can see all the smaller coins which had lower price.
4.Scatter Plot :
  • For the Scatter Plot, let us compare all the currencies in terms of its of volume and price.
  • px.scatter(df, x="Close", y="Volume", color="Currency", hover_data=["High", "Low"], height=400, log_x="True", log_y="True")

->The above code gives us a Scatter Plot with px.scatter will create a scatter plot, df is the data, horizontal x axis is x i.e. Close and vertical y axis is y i.e. Volume.

->The additional parameter here is color which is set to Currency, this gives unique colors for every currencies.

->Another parameter here is hover_data which is set to High and Low this gives us the high price and low price of the currencies when we hover over them.

->Height=400 sets the height of the graph you can play with the values give anything you want.

->log_x="True" and log_y="True" showes us the clear graph even the smaller coins.

cc90ccd59bc4140d61e0c7c81cbb8e66d53cfc1b20fd945ac679b2722c9d8581.png

 

  • Above we have created a Scatter Plot, smaller coins are also visible in this like tether, it has highest volume but the price is constant at 1USD.
  • We can hover over the graph to see the values
  • There is Bitcoin in Blue color having highest price and volume.
  • Every currency above has its own color.
  • By this we can observe all the coins correctly.
Importing Datasets  : Bitcoin
  • Let us analyze bitcoin using different graphs, this is called crypto-specific data visualization.
  • Here, we will analyze single cryptocurrency.

First, let us import the data for Bitcoin, we can download it from any crypto related sites.

Here is the link to the file which I have used in this : http://upload-4ever.com/d/HyJk

  • Import the file as done before for top coins.
  • from google.colab import files
  • files.upload()

  • You can view the file by,
  • df= pd.read_csv("top_coins.csv")
  • df
1.Line graph :
  • In this let us create the Line graph to observe the historical performance on the price of bitcoin, from 2014.
  • Code,
  • px.line(df, x="Date", y="Close") 

->This is all we need to create a line graph. px.line() is used to create a line graph, df is the Data, will be the horizontal x axis for the graph which is the Date column and y will be the vertical y axis which will be the Close price i.e. Close column.

c995ef768081bee75703645d01a0ff9131839bd7288fd0af3ea12e509663bf73.png

  • Above we have is the Line graph, we can see that there was an explosion after early 2017 in terms of its price.
  • If we hover over the graph we can see their price.
2. Area Graph :
  • Let's create an Area graph to look at the transaction volume of bitcoin.
  • Code,
  • px.area(df, x="Date", y="Volume")

->This is all we need to create the Area graph. px.area() is used to create a Area graph, df is the Data, x will be the horizontal x axis for the graph which is the Date column and y will be the vertical y axis which will be the Volume column.

06d58c045e4c6e69bf983c80bbf9263a87e45f6134f876cb7efaca42a15ccebf.png

  • By the above graph we can see that the transaction volume of the bitcoin is increasing rate, we can see there is a peak in 2020, it was when the stock market crashed and during lockdown.

You can also download the graph you made by clicking on Download plot as png option above the graph,

7bc4caeb9e7b053f9e3ba2faa5fd1d0d73d215e35a740f3dd87dd405dc503777.jpg

This was all, Thank you for showing some patience and reading my posts, hope you understand this and hope you will learn how to create your own graphs after reading this post.

  • You can mail me if you have any doubts regarding this post.
  • Don't forget to like.

NOTE :

All the graphs posted above are the works done by me.


Sumukh
Sumukh

I'm an Computer Science Engineer and Programmer with lots of interest in cryptocurrencies and want to explore more by reading some of the very useful and interesting topics here and also publish some of mine.


CryptoBomb
CryptoBomb

All about cryptocurrencies... +_+

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.