Introduction
In this tutorial, we will learn how to create a scatter plot with varying marker colors and sizes using Python Matplotlib.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import necessary libraries
We will start by importing the necessary libraries, which are Matplotlib and Numpy.
import matplotlib.pyplot as plt
import numpy as np
Load data
We will load a numpy record array from yahoo csv data with fields date, open, high, low, close, volume, adj_close from the mpl-data/sample_data directory. The record array stores the date as an np.datetime64 with a day unit ('D') in the date column.
import matplotlib.cbook as cbook
price_data = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
price_data = price_data[-250:] ## get the most recent 250 trading days
Calculate values for scatter plot
We will calculate delta1, volume, and close values for scatter plot.
delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1]
## Marker size in units of points^2
volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]
Create scatter plot
We will create a scatter plot with varying marker colors and sizes using the calculated values.
fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
ax.set_xlabel(r'$\Delta_i$', fontsize=15)
ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
ax.set_title('Volume and percent change')
ax.grid(True)
fig.tight_layout()
plt.show()
Summary
We have learned how to create a scatter plot with varying marker colors and sizes using Python Matplotlib. We started by importing the necessary libraries, then loading data, calculating values for scatter plot, and creating the scatter plot.