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.
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,
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.
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.
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.
Now, run this.
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
->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.
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
- 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,
->This groups the table in terms of Currencies and counts the total number of rows present in it.
- 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.
- 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.
- 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.
- 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")
- 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.
- 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
- You can view the file by,
- df= pd.read_csv("top_coins.csv")
1.Line graph :
- In this let us create the Line graph to observe the historical performance on the price of bitcoin, from 2014.
- 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, 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 Close price i.e. Close column.
- 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.
- 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.
- 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,
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.
All the graphs posted above are the works done by me.