Path: blob/master/Documentation/Python/Plotting-Price-Data-Using-Matplotlib.ipynb
984 views
![]()
Visualizing Data with Matplotlib
Line Plots, Histograms, Scatter plots, and Heat Maps
Import libraries
Let's start by importing the libraries we need.
Visualizing SPY
We will visualize SPY using a time-series line plot and a histogram.
Gathering Data
To begin, we retrieve daily close data for SPY
Here, we visualize SPY with a time-series line plot. Note that pandas has built-in support for matplotlib, making it easier and quicker for us to create plots.
Next, we want to visualize the returns distribution of SPY. We call pct_change() on our SPY Series to get the daily returns.
Visualizing the Correlation Between GLD and BAR Returns
GLD and BAR are two different Gold-tracking ETFs, and we would like to visualize their correlation. To do this, we will employ a scatter plot of their returns.
Gathering Data
First, we get daily close returns data for GLD and BAR
Since we would like to include a trend line (OLS) in our scatter plot, we will need to employ numpy to calculate our OLS line.
Visualizing Correlations Between Banking Stocks
Correlations between stocks in the same sector are commonly applied to develop pairs trading strategies. We will visualize the correlations between a few stocks in the banking sector using a heat map. Note: we strongly advise using Seaborn instead for heat maps, as it needs only roughly 1/10th of the number of lines of code.
Gathering Data
First, we get historical closing prices for four banking sector stocks.
Before we create the heat map, we must create a correlation matrix, which has all the correlations between all possible pairs of symbols. This can be easily done with pandas DataFrame's built-in corr() method.
With the correlation matrix, we are ready to create a heat map.