Making Animated Bar Plots of Top 16 CryptoCurrencies (Market Capitalization, Price and Volume)
How to make animation in python using matplotlib package?
Being a data scientist become more exciting job when you get to work in blockchain industry. As blockchain industry is booming, scope of data science and machine learning also increasing . To apply data science and machine learning to blockchain industry, basis understanding of blockchain is must. However, I won’t cover anything about blockchain in this article.
Recently, I have been exploring trading data of cryptocurrencies on different exchanges and crypto analysis platforms. There are more than 4000 crypto coins in market. Bitcoin dominates crypto market which holds 52% of total cryptocurrency market capitalization. Ethereum (11%) and Ripple (10%) are second and third as per coingecko.
Let’s make animated bar plot video of top 16 crypto currencies market cap-wise. Market capitalization of cryptocurrency is equal to multiplication of total supply of crypto coin and per coin price.
Market capitalization = Total supply of coins × Price per coin
I have used Coinmarketcap historical data for this tutorial. You can extract market capitalization , price and volume data of cryptocurrencies from Coinmarketcap using its API. I have collected market capitalization data of top 16 cryptocurrencies for last 600 days from Coinmarketcap. Figure 1 shows historical market capitalization data sample of top 16 cryptocurrencies.
Matplotlib, seaborn, and ploty are most popular visualization python packages among data scientists. Matplotlib is used for this tutorial. Now, I will explain whole process step by step.
Step 1. Import pandas, matplotlib.pyplot and matplotlib.animation.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Step 2. Read CSV data file and do some data processing and cleaning.
crypto_cap = pd.read_csv('crypto_market_cap.csv') # Read CSV file
crypto_cap.index = crypto_cap['date'] # Set index as date index
crypto_cap = crypto_cap.drop(['date'],axis=1) # Remove date column
crypto_cap1 = crypto_cap.T # Get Transpose of DataFrame to make date as columns
crypto_cap1.index = crypto_cap1.index.str.upper() # Covert coins names to uppercase
Step 3. select a writer to write animation and define writer’s parameters.
Writer = animation.writers['ffmpeg'] # Select writer
writer = Writer(fps=20, metadata=dict(artist='D C Aichara')) # Writer's parameters
Step 4. Define figure and add subplots
fig, ax = plt.subplots(figsize =(16,12))
Step 5. Define animate function.
Animate function includes data range (for our data one day ), plot definition, plot labels, axis labels, texts etc.
Note 1. Use ax.clear() before plot definition to avoid overlap of plots in animation.
Note 2. You can customize plot as per you requirements and preferences.
Step 6. Create animation using FuncAnimation.
ani = animation.FuncAnimation(fig, animate, interval =100, frames =575, repeat =False) # create animation
Interval in FuncAnimation is time per frame in milliseconds.
Frames = Number of plots (or number number of columns in our case).
repeat = False to stop animation at the end of frames.
Step 7. Save Animation.
ani.save('crypto_market_cap.mp4') # save Animation
Our animation of bar plots is ready. Let’s watch it.
In same way you can make animation of bar plots of historical price and volume data.
I hope , you got pretty much idea about how to make animation of bar plots using matplotlib through tutorial. You can find complete code on Github. Please, reach out to me on LinkedIn or Twitter, if you have any query.
P.S. :If you are into blockchain and cryptocurrencies analysis, please join our Telegram Channel.