How to Retrieve and Visualize Financial Time‑Series Data with Python, Pandas, and Matplotlib
This guide shows how to fetch financial time‑series data from Quandl using Python, store it in pandas, explore it with head/tail, and create line, volume, and candlestick visualizations with matplotlib, including code snippets and best practices for API keys and date handling.
Quandl is a platform providing financial, economic, and alternative data from various publishers such as the UN, World Bank, central banks, and exchanges.
Using the Python quandl module you can import data easily; free datasets are available, while premium data requires a paid subscription.
To retrieve the ABN dataset from the Euronext exchange, set your API key and run:
import quandl
QUANDL_API_KEY = "YOUR_API_KEY"
quandl.ApiConfig.api_key = QUANDL_API_KEY
df = quandl.get('EURONEXT/ABN')Storing the API key in a constant makes future changes simple. The data is loaded into a pandas DataFrame. You can inspect the first and last rows with df.head() and df.tail(), and adjust the number of rows displayed by passing an integer argument.
By default the data is plotted with df.plot(), which uses matplotlib. Ensure %matplotlib inline is executed and that the matplotlib library is installed.
To visualize the relationship between closing price and volume, extract the relevant columns:
prices = df['Last']
volumes = df['Volume']Then create a two‑row subplot grid:
import matplotlib.pyplot as plt
%matplotlib inline
top = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4)
top.plot(prices.index, prices, label='Last')
plt.title('ABN Last Price from 2015‑2018')
plt.legend(loc=2)
bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
bottom.bar(volumes.index, volumes)
plt.title('ABN Daily Trading Volume')
plt.gcf().set_size_inches(12, 8)
plt.subplots_adjust(hspace=0.75)The resulting figure shows price trends and trading volume.
For a more detailed view, a candlestick chart can be drawn using the mpl_finance (or mplfinance) package:
%matplotlib inline
import quandl
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
df_subset = quandl.get('EURONEXT/ABN', start_date='2018-07-01', end_date='2018-07-31')
df_subset['Date'] = df_subset.index.map(mdates.date2num)
df_ohlc = df_subset[['Date','Open','High','Low','Last']]
fig, ax = plt.subplots(figsize=(8,4))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
candlestick_ohlc(ax, df_ohlc.values, width=0.8, colorup='green', colordown='red')
plt.show()The candlestick chart displays open, high, low, and close prices for each day, offering richer information than a simple line chart.
Note: You can specify start_date and end_date in quandl.get() to limit the time range of the dataset.
This article is excerpted from "Python Financial Data Analysis (2nd edition)" with permission.
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.
