Creating Animated Bitcoin Area Charts with Matplotlib: A Step‑by‑Step Guide
This article demonstrates how to import Bitcoin price data, preprocess it, and use Matplotlib's fill_between, bar, and FuncAnimation functions to generate static and animated area charts, including code snippets, parameter explanations, and tips for exporting the animation as GIF or MP4.
We use Bitcoin price data from 2013 to 2019 to create an animated area chart. The dataset contains columns for year, city name, group, and value, and a transposed version provides daily open, high, low, and close prices starting from 2013‑04‑28.
First, we import the CSV file BTC_price_history.csv and convert the date column to datetime. The daily price is calculated as the average of the high and low values.
We set Span_Date = 180 to select a 180‑day window, creating a temporary DataFrame df_temp. Using plt.fill_between() produces a solid‑filled area chart (Figure a), while plt.bar() with the Spectral_r colormap creates a gradient‑filled area chart (Figure b). The relevant code snippets are:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('BTC_price_history.csv')
df['date'] = pd.to_datetime(df['date'])
df['Price'] = (df['High'] + df['Low']) / 2
Span_Date = 180
df_temp = df.tail(Span_Date)
plt.fill_between(df_temp['date'], df_temp['Price'], color='red')
plt.bar(df_temp['date'], df_temp['Price'], color=plt.cm.Spectral_r(df_temp['Price']))We encapsulate the static chart code into a function draw_areachart(Num_Date). When the start date is earlier than the current date, the function draws the area chart up to that date; otherwise it draws up to the latest available data. Different start dates generate different area charts.
To animate the chart, we use matplotlib.animation.FuncAnimation:
from matplotlib import animation
import numpy as np
fig, ax = plt.subplots()
Num_Date = np.arange(0, df.shape[0], 1)
animator = animation.FuncAnimation(fig, draw_areachart, frames=Num_Date, init_func=None, interval=200, blit=True)
from IPython.display import HTML
HTML(animator.to_jshtml())The animation shows the evolving area chart over time (Figure 10). Note that the default embed size limit is 20 MB, so the example only covers data from 2013‑04 to 2014‑07; adjust animation.embed_limit to increase the limit.
Animations can be saved as GIF or MP4 using animator.save('animation.gif') or animator.save('animation.mp4'). Exporting MP4 requires ffmpeg or mencoder installed.
The source code and data are available on GitHub: https://github.com/EasyChart/Beautiful-Visualization-with-python .
Both matplotlib and plotnine can create dynamic visualizations. In matplotlib, FuncAnimation(fig, func, frames, init_func, interval, blit) is the main function, where fig is the canvas, func is the custom drawing function (e.g., draw_barchart or draw_areachart), frames is the number of frames, init_func is optional, interval is the update frequency in milliseconds, and blit should be True except on macOS where False is required. plotnine 's PlotnineAnimation() can also produce animated charts but is slower for continuously updating data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
